description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input().split("*")
ans = -1
for i in range(len(s)):
for j in range(0, len(s)):
ans = max(
ans,
eval(
"*".join(
s[:i] + [str(eval("*".join(s[i : i + j + 1])))] + s[i + j + 1 :]
)
),
)
print(ans) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL STRING BIN_OP BIN_OP VAR VAR LIST FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input()
mult_list = []
for i in range(len(s)):
if s[i] == "*":
mult_list.append(i)
best = eval(s)
for k in mult_list:
t = "(" + s[:k] + ")" + s[k:]
q = eval(t)
if q > best:
best = q
t = s[: k + 1] + "(" + s[k + 1 :] + ")"
q = eval(t)
if q > best:
best = q
for j in mult_list:
if j >= k:
continue
if j < k:
t = s[: j + 1] + "(" + s[j + 1 : k] + ")" + s[k:]
q = eval(t)
if q > best:
best = q
print(best) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING VAR VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | str = "1*" + input() + "*1"
l = len(str)
ans = 0
for i in range(2, l, 2):
if str[i - 1] == "*":
for j in range(i + 1, l, 2):
if str[j] == "*":
ans = max(ans, eval(str[:i] + "(" + str[i:j] + ")" + str[j:]))
print(ans) | ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input()
s = "1*" + s + "*1"
IL = []
for i in range(len(s)):
if s[i] == "*":
IL.append(i)
maks = 0
for i in range(len(IL)):
for j in range(i + 1, len(IL)):
a = eval(s[: IL[i] + 1] + "(" + s[IL[i] + 1 : IL[j]] + ")" + s[IL[j] :])
maks = max(maks, a)
print(maks) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER STRING VAR BIN_OP VAR VAR NUMBER VAR VAR STRING VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | src_str = input()
lenstr = len(src_str)
str = list(src_str)
listMulPos = []
for i, c in enumerate(str):
if c == "*":
listMulPos.append(i)
listLeftParPos = [0]
for i in listMulPos:
listLeftParPos.append(i + 1)
listRightParPos = [lenstr]
for i in listMulPos:
listRightParPos.append(i)
ans = 0
for leftPos in listLeftParPos:
for rightPos in listRightParPos:
tmp = []
for i in str:
tmp.append(i)
if leftPos < rightPos + 1:
tmp.insert(leftPos, "(")
tmp.insert(rightPos + 1, ")")
tmpval = eval("".join(tmp))
ans = max(ans, tmpval)
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input()
mx = eval(s)
m = [-1] + [i for i, v in enumerate(s) if v == "*"] + [len(s)]
for i, a in enumerate(m):
for b in m[i + 1 :]:
t = s[: a + 1] + "(" + s[a + 1 : b] + ")" + s[b:]
mx = max(mx, eval(t))
print(mx) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR STRING LIST FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = "1*" + input() + "*1"
mx = 0
v = []
for i in range(len(s)):
if s[i] == "*":
v.append(i)
for i in range(len(v)):
for j in range(i + 1, len(v)):
z = eval(s[v[i] + 1 : v[j]])
mx = max(mx, eval(s[: v[i] + 1] + str(z) + s[v[j] :]))
print(mx) | ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | expr = list(input())
mul_pos = [-1]
add_cnt = [0] * len(expr)
for i, _ in enumerate(expr):
add_cnt[i] = add_cnt[i - 1]
if _ == "*":
mul_pos.append(i)
elif _ == "+":
add_cnt[i] += 1
mul_pos.append(len(expr))
add_cnt.append(add_cnt[-1])
add_cnt.append(0)
ans = 0
for i in range(len(mul_pos)):
for j in range(i + 1, len(mul_pos)):
if add_cnt[mul_pos[i]] - add_cnt[mul_pos[j]]:
expr.insert(mul_pos[i] + 1, "(")
expr.insert(mul_pos[j] + 1, ")")
ans = max(ans, eval("".join(expr)))
expr.pop(mul_pos[j] + 1)
expr.pop(mul_pos[i] + 1)
print(eval("".join(expr)) if ans == 0 else ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL STRING VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input()
s = "0+1*" + s + "*1+0"
answer = eval(s)
n = len(s)
p = []
for i in range(0, n):
if s[i] == "*":
p.append(i)
for i in p:
for j in p:
if i >= j:
continue
test = eval(s[: i + 1] + "(" + s[i + 1 : j] + ")" + s[j:])
if test > answer:
answer = test
print(answer) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR STRING VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | __author__ = "artbataev"
def countExpression(i, j):
global matr, numbers, operations
if i == 0 or operations[i - 1] == "+":
return matr[j] - (0 if i == 0 else matr[i - 1])
cur = numbers[i]
k = i
while k < j and operations[k] == "*":
cur *= numbers[k + 1]
k += 1
if k >= j:
return cur
else:
return cur + matr[j] - matr[k]
s = input().strip()
numbers = list(map(int, s[::2]))
operations = s[1::2]
n = len(numbers)
matr = [(0) for i in range(n)]
matr[0] = numbers[0]
for i in range(1, n):
if operations[i - 1] == "+":
matr[i] = matr[i - 1] + numbers[i]
else:
k = i - 1
cur = numbers[i]
while k >= 0 and operations[k] == "*":
cur *= numbers[k]
k -= 1
if k < 0:
matr[i] = cur
else:
matr[i] = matr[k] + cur
maxRes = matr[n - 1]
for i in range(n - 1):
if i == 0 or operations[i - 1] == "*":
for j in range(i + 1, n):
curExpr = countExpression(i, j)
i1 = i - 1
while i1 >= 0 and operations[i1] == "*":
curExpr *= numbers[i1]
i1 -= 1
j1 = j
while j1 < n - 1 and operations[j1] == "*":
curExpr *= numbers[j1 + 1]
j1 += 1
if i1 >= 0:
curExpr += matr[i1]
if j1 + 1 <= n - 1:
curExpr += matr[n - 1] - matr[j1]
maxRes = max(maxRes, curExpr)
print(maxRes) | ASSIGN VAR STRING FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING RETURN BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR STRING VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR VAR STRING VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR STRING VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input()
processed = True
i1 = 1
while i1 < len(s):
while i1 < len(s):
if s[i1] == "+":
break
i1 += 2
if i1 >= len(s):
break
i2 = i1 + 2
while i2 < len(s):
if s[i2] == "*":
break
i2 += 2
i2 = i2 - 2
if i2 - i1 > 2:
s1 = eval(s[i1 + 1 : i2])
s = s[: i1 + 1] + str(s1) + s[i2:]
i1 += 4
else:
i1 += 2
mults = [-1]
for i in range(len(s)):
if s[i] == "*":
mults.append(i)
mults.append(len(s))
max = 0
for i in range(len(mults)):
for j in range(i + 1, len(mults)):
news = (
s[: mults[i] + 1] + "(" + s[mults[i] + 1 : mults[j]] + ")" + s[mults[j] :]
)
v = eval(news)
if v > max:
max = v
print(max) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER STRING VAR BIN_OP VAR VAR NUMBER VAR VAR STRING VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | from itertools import combinations
s = str(input())
ans = eval(s)
op = [-1] + [i for i, d in enumerate(s) if d == "*"] + [len(s)]
for i, j in combinations(op, 2):
ans = max(ans, eval("{}({}){}".format(s[: i + 1], s[i + 1 : j], s[j:])))
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR STRING LIST FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input()
length = len(s)
ans = eval(s)
for j in range(0, length, 2):
if j == 0 or s[j - 1] == "*":
for k in range(j + 3, length + 1, 2):
if k == length or s[k] == "*":
tmp = eval(s[:j] + str(eval(s[j:k])) + s[k:])
ans = ans if tmp < ans else tmp
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | S = "1*" + input() + "*1"
n = len(S)
ans = eval(S)
for i in range(2, n + 1, 2):
if S[i - 1] == "*":
for j in range(i + 1, n, 2):
if S[j] == "*":
T = S[0:i] + "(" + S[i:j] + ")" + S[j:]
ans = max(ans, eval(T))
print(ans) | ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR STRING VAR VAR VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input()
d = list(s.split("*"))
last = 0
po = []
ki = []
for i in range(len(d)):
po += [last]
ki += [len(d[i]) + last]
last += len(d[i]) + 1
ans = [eval(s)]
for a in po:
for b in ki:
if a > b - 2:
continue
x = s[:a] + "(" + s[a:b] + ")" + s[b:]
ans += [eval(x)]
print(max(ans)) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR LIST VAR VAR LIST BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR VAR VAR STRING VAR VAR VAR LIST FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | import sys
def f2(exp: str, index1: int, index2: int):
return eval(exp[:index1] + "(" + exp[index1 : index2 + 1] + ")" + exp[index2 + 1 :])
def f1(exp: str):
res, pos = eval(exp), [-1]
for i in range(1, len(exp), 2):
if exp[i] == "*":
pos.append(i)
pos.append(len(exp))
for i in range(0, len(pos)):
for j in range(i + 1, len(pos)):
res = max(res, f2(exp, pos[i] + 1, pos[j] - 1))
return res
for line in sys.stdin:
print(f1(line)) | IMPORT FUNC_DEF VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER FUNC_DEF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER RETURN VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | S = input()
N = len(S)
Ans = eval(S)
for i in range(0, N, 2):
for j in range(i, N, 2):
if (i == 0 or i > 0 and S[i - 1] == "*") and (
j == N - 1 or j < N - 1 and S[j + 1] == "*"
):
SS = S[:i] + str(eval(S[i : j + 1])) + S[j + 1 :]
if int(eval(SS)) > Ans:
Ans = int(eval(SS))
print(Ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input()
if len(s) == 1:
print(s)
else:
ans = eval(s)
l = []
for i in range(len(s)):
if s[i] == "*":
l += [i]
if len(l) == 0:
print(ans)
else:
l = [-1] + l
l = l + [len(s)]
for i in range(len(l)):
for j in range(i + 1, len(l)):
temp = s[: l[i] + 1]
temp += "("
temp += s[l[i] + 1 : l[j]]
temp += ")"
temp += s[l[j] :]
ans = max(ans, eval(temp))
print(ans) | ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR LIST VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR LIST FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR STRING VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR STRING VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = "1*" + input() + "*1"
a = ""
n = len(s)
s = s + "+"
i = 0
while i < n:
if s[i + 1] == "*":
while s[i + 1] == "*":
a += s[i : i + 2]
i += 2
a += s[i : i + 2]
i += 2
else:
sum = 0
while i < n and s[i + 1] == "+":
sum += int(s[i])
i += 2
a += str(sum) + "+"
a = a[0:-1]
ans = 0
for j in range(len(a)):
for i in range(j):
if (a[i] == "*" or a[i] == "+") and (a[j] == "*" or a[j] == "+"):
ans = max(eval(a[: i + 1] + "(" + a[i + 1 : j] + ")" + a[j:]), ans)
print(ans) | ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER STRING WHILE VAR BIN_OP VAR NUMBER STRING VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER STRING VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR STRING VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | str = "1*" + input() + "*1"
n = len(str)
max_z = eval(str)
arr = []
for i in range(1, n, 2):
if str[i] == "*":
arr.append(i)
for i in range(len(arr)):
s1 = str[: arr[i] + 1] + "(" + str[arr[i] + 1 :]
for j in range(i + 1, len(arr)):
s2 = s1[: arr[j] + 1] + ")" + s1[arr[j] + 1 :]
max_z = max(eval(s2), max_z)
print(max_z) | ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER STRING VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER STRING VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | expr = input()
ans = eval(expr)
factor = expr.split("*")
for i in range(1, len(factor)):
ans = max(ans, eval("(" + "*".join(factor[0:i]) + ")*" + "*".join(factor[i:])))
ans = max(ans, eval("*".join(factor[0:i]) + "*(" + "*".join(factor[i:]) + ")"))
for j in range(i + 1, len(factor)):
ans = max(
ans,
eval(
"*".join(factor[0:i])
+ "*("
+ "*".join(factor[i:j])
+ ")*"
+ "*".join(factor[j:])
),
)
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING FUNC_CALL STRING VAR NUMBER VAR STRING FUNC_CALL STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL STRING VAR NUMBER VAR STRING FUNC_CALL STRING VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL STRING VAR NUMBER VAR STRING FUNC_CALL STRING VAR VAR VAR STRING FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input()
res = eval(s)
n = len(s)
for i in range(-1, n):
if i == -1 or s[i] == "*":
for j in range(i + 1, n + 1):
if j == n or s[j] == "*":
new_s = s[0 : i + 1] + "(" + s[i + 1 : j] + ")" + s[j:n]
res = max(res, eval(new_s))
print(res) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR STRING VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input()
pos = [-1]
for i, x in enumerate(s):
if x == "*":
pos.append(i)
pos.append(len(s))
max_v = 0
for i in range(len(pos) - 1):
for j in range(i + 1, len(pos)):
a = pos[i] + 1
b = pos[j]
ns = s[:a] + "(" + s[a:b] + ")" + s[b:]
e = eval(ns)
if e > max_v:
max_v = e
print(max_v) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR VAR VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input()
t = len(s)
res = eval(s)
for i in range(0, t):
if i != 0 and s[i - 1] != "*":
continue
a = s[:i] + "(" + s[i:]
for j in range(i, t + 2):
if j != t + 1 and a[j] != "*":
continue
if j == t + 1:
b = a[:j] + ")"
else:
b = a[:j] + ")" + a[j:]
res = max(res, eval(b))
print(res) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input()
ans = eval(s)
n = len(s)
pos = [i for i, x in enumerate(s) if x == "*"]
for x in pos:
s2 = f"({s[0:x]}){s[x:]}"
ans = max(ans, eval(s2))
s2 = f"{s[:x + 1]}({s[x + 1:]})"
ans = max(ans, eval(s2))
for y in pos:
if y >= x:
continue
s2 = f"{s[:y + 1]}({s[y + 1:x]}){s[x:]}"
ans = max(ans, eval(s2))
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR STRING FOR VAR VAR ASSIGN VAR STRING VAR NUMBER VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | maxx = 0
x = []
s = input()
maxx = max(maxx, eval(s))
for i in range(len(s)):
if s[i] == "*":
x.append(i)
maxx = max(maxx, eval("(" + s[:i] + ")" + s[i:]))
maxx = max(maxx, eval(s[: i + 1] + "(" + s[i + 1 :] + ")"))
for f in range(len(x)):
for g in range(f + 1, len(x)):
i = x[f]
j = x[g]
maxx = max(maxx, eval(s[: i + 1] + "(" + s[i + 1 : j] + ")" + s[j:]))
print(maxx) | ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING VAR VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = "*" + input() + "*"
n = len(s)
ans = eval(s[1 : n - 1])
for i in range(n):
if s[i] != "*":
continue
for j in range(i + 1, n):
if s[j] != "*":
continue
r = s[1 : i + 1] + "(" + s[i + 1 : j] + ")" + s[j : n - 1]
ans = max(ans, eval(r))
print(ans) | ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR STRING VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | exp = input()
if exp.count("+") == 0:
print(eval(exp))
elif exp.count("*") == 0:
print(eval(exp))
else:
maximum = 0
exp = "1*" + exp + "*1"
for index1 in range(0, len(exp)):
value = 0
if exp[index1 : index1 + 1] == "*":
for index2 in range(index1 + 1, len(exp)):
if exp[index2 : index2 + 1] == "*":
value = eval(exp[index1 + 1 : index2])
sub_exp = exp[0 : index1 + 1] + str(value) + exp[index2:]
value = eval(sub_exp)
if value > maximum:
maximum = value
print(maximum) | ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input()
n = len(s)
ans = eval(s)
for i in range(0, n):
for j in range(i + 1, n):
if s[i] == "*" and s[j] == "*":
e = s[0 : i + 1] + "(" + s[i + 1 : j] + ")" + s[j:]
ans = max(ans, eval(e))
for i in range(0, n):
if s[i] == "*":
e = "(" + s[0:i] + ")" + s[i:]
ans = max(ans, eval(e))
e = s[0 : i + 1] + "(" + s[i + 1 :] + ")"
ans = max(ans, eval(e))
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING VAR NUMBER VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = "1*" + input() + "*1"
n = len(s)
ans = eval(s)
pos = []
for i in range(n):
if s[i] == "*":
pos.append(i)
for i in [(x + 1) for x in pos]:
for j in [x for x in pos]:
if i < j:
ans = max(ans, eval(s[:i] + "(" + s[i:j] + ")" + s[j:]))
print(ans) | ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | S = input()
ans = 0
for ll in range(len(S)):
if not (ll == 0 or S[ll - 1] == "*"):
continue
for rr in range(ll + 1, len(S) + 1, 1):
if not (rr == len(S) or S[rr] == "*"):
continue
ans = max(ans, eval(S[:ll] + "(" + S[ll:rr] + ")" + S[rr:]))
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input()
s = "m = max(m, " + s + ")"
pu = [10]
for a in range(len(s)):
if s[a] == "*":
pu += [a]
pu += [len(s)]
m = 0
exec(s)
for i in range(len(pu)):
for j in range(i + 1, len(pu)):
exec(s[: pu[i] + 1] + "(" + s[pu[i] + 1 : pu[j]] + ")" + s[pu[j] :])
print(m) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR STRING ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR LIST VAR VAR LIST FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER STRING VAR BIN_OP VAR VAR NUMBER VAR VAR STRING VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | def main():
s = input()
n = len(s)
ans = -(10**18)
for i in range(1, n + 1, 2):
for j in range(0, i, 2):
if (j == 0 or s[j - 1] == "*") and (i == n or s[i] == "*"):
ans = max(ans, eval(s[:j] + "(" + s[j:i] + ")" + s[i:]))
print(ans)
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input()
exec("ans = " + s)
n = s.count("*")
last_s = s
for i in range(n + 1):
for j in range(i + 1, n + 2):
new_s = ""
if i == 0:
new_s += "("
k = 1
for elem in s:
if elem == "*":
if i == k:
new_s += "*("
elif j == k:
new_s += ")*"
else:
new_s += "*"
k += 1
else:
new_s += elem
if j == n + 1:
new_s += ")"
exec("_ans = " + new_s)
if _ans > ans:
ans = _ans
last_s = new_s
print(ans) | ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR VAR VAR STRING IF VAR VAR VAR STRING VAR STRING VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input()
ans = eval(s)
for i in range(len(s) - 1):
if s[i] == "*":
k = "(" + s[0:i] + ")" + s[i : len(s)]
t = eval(k)
if t > ans:
ans = t
k = s[0 : i + 1] + "(" + s[i + 1 : len(s)] + ")"
t = eval(k)
if t > ans:
ans = t
for j in range(i + 2, len(s)):
if s[j] == "*":
k = s[0 : i + 1] + "(" + s[i + 1 : j] + ")" + s[j : len(s)]
t = eval(k)
if t > ans:
ans = t
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING VAR NUMBER VAR STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | qw = input()
g = ["*"]
s = [1]
for i in range(len(qw)):
if i % 2 == 0:
s.append(int(qw[i]))
else:
g.append(qw[i])
c = 0
g.append("*")
s.append(1)
for i in range(len(g)):
if g[i] == "*":
for j in range(i + 1, len(g)):
if g[j] == "*":
a = 0
b = s[0]
for k in range(i):
if g[k] == "*":
b *= s[k + 1]
else:
a += b
b = s[k + 1]
v = a
w = b
a = 0
b = s[i + 1]
for k in range(i + 1, j):
if g[k] == "*":
b *= s[k + 1]
else:
a += b
b = s[k + 1]
b = w * (a + b)
a = v
for k in range(j, len(g)):
if g[k] == "*":
b *= s[k + 1]
else:
a += b
b = s[k + 1]
c = max(c, a + b)
print(c) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | def mul2val(s):
res = 1
for i in s.split("*"):
res *= int(i)
return res
s = "1*" + input() + "*1"
muls = s.split("+")
vals = list(map(mul2val, muls))
lvals = list(map(mul2val, [(mul[:-2] if len(mul) > 1 else "0") for mul in muls]))
rvals = list(map(mul2val, [(mul[2:] if len(mul) > 1 else "0") for mul in muls]))
cumvals = [0] + vals
for i in range(1, len(cumvals)):
cumvals[i] += cumvals[i - 1]
total = cumvals[-1]
ans = total
for i, mul1 in enumerate(muls):
if len(mul1) == 1:
continue
for j, mul2 in enumerate(muls[i + 1 :], i + 1):
if len(mul2) == 1:
continue
tval = (
cumvals[i]
+ lvals[i]
* (int(mul1[-1]) + (cumvals[j] - cumvals[i + 1]) + int(mul2[0]))
* rvals[j]
+ (total - cumvals[j + 1])
)
if tval > ans:
ans = tval
print(ans) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input()
lb, rb = [], []
n = len(s)
lb.append(0)
rb.append(n)
for i in range(n):
if s[i] == "*":
lb.append(i + 1)
rb.append(i)
ans = 0
for l in lb:
for r in rb:
if l >= r:
continue
t = s[:l] + "(" + s[l:r] + ")" + s[r:]
ans = max(ans, eval(t))
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR VAR VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | def find_best(s):
best = eval(s)
for i in range(0, len(s), 2):
if i > 0 and s[i - 1] == "*" or i == 0:
for j in range(i + 2, len(s), 2):
if j < len(s) - 1 and s[j + 1] == "*" or j == len(s) - 1:
sp = s[:i] + "(" + s[i : j + 1] + ")" + s[j + 1 :]
best = max(best, eval(sp))
return best
s = input()
print(find_best(s)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Vanya is doing his maths homework. He has an expression of form $x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
-----Input-----
The first line contains expression s (1 β€ |s| β€ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
-----Output-----
In the first line print the maximum possible value of an expression.
-----Examples-----
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
-----Note-----
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60). | s = input()
mults = [-1]
for i in range(len(s)):
if s[i] == "*":
mults.append(i)
mults.append(len(s))
max = 0
for i in range(len(mults)):
for j in range(i + 1, len(mults)):
news = (
s[: mults[i] + 1] + "(" + s[mults[i] + 1 : mults[j]] + ")" + s[mults[j] :]
)
v = eval(news)
if v > max:
max = v
print(max) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER STRING VAR BIN_OP VAR VAR NUMBER VAR VAR STRING VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | n, k = map(int, input().split())
s = list(input())
if n == 1:
print(0)
print(s[0])
exit()
if k == 2:
a, b = "A", "B"
x, y = 0, 0
for i in range(n):
item = s[i]
if i % 2:
if item == "A":
x += 1
else:
y += 1
elif item == "B":
x += 1
else:
y += 1
if x > y:
print(y)
print("BA" * (n // 2) + "B" * (n % 2))
else:
print(x)
print("AB" * (n // 2) + "A" * (n % 2))
else:
a, b, c = "A", "B", "C"
ans = 0
for i in range(1, n - 1):
if s[i] == s[i - 1]:
x, y = s[i - 1], s[i + 1]
if a != x and a != y:
s[i] = a
ans += 1
elif b != x and b != y:
s[i] = b
ans += 1
else:
s[i] = c
ans += 1
if s[-1] == s[-2]:
ans += 1
if s[-2] == a:
s[-1] = b
else:
s[-1] = a
print(ans)
print("".join(s)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR STRING STRING ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR VAR VAR STRING STRING STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | n, k = map(int, input().split())
s = list(input())
if k <= 2:
f = lambda t: sum(int(t[i] != s[i]) for i in range(n))
x, y = "AB" * n, "BA" * n
res_x, res_y = f(x), f(y)
if res_x > res_y:
print(res_y)
print(y[:n])
else:
print(res_x)
print(x[:n])
exit()
res = 0
for i in range(1, n):
if s[i - 1] == s[i]:
res += 1
s[i] = list(set("ABC") - set(s[i : i + 2]))[0]
print(res)
print("".join(s)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR BIN_OP STRING VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | n, k = [int(x) for x in input().split()]
s = input()
if k == 2:
str1 = "AB" * n
str2 = "BA" * n
diff1 = 0
diff2 = 0
for i in range(n):
if s[i] != str1[i]:
diff1 += 1
else:
diff2 += 1
if diff1 < diff2:
print(diff1)
print(str1[:n])
else:
print(diff2)
print(str2[:n])
else:
l = [chr(i + ord("A")) for i in range(k)]
s += "_"
s = "_" + s
s = list(s)
resVal = 0
for i in range(1, n + 1):
if s[i] != s[i - 1]:
continue
resVal += 1
possible_changes = [x for x in l if x != s[i - 1] and x != s[i + 1]]
s[i] = possible_changes[0]
del s[0]
del s[-1]
s = "".join(s)
print(resVal)
print(s) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | n, k = map(int, input().split())
s = input()
l = []
ans = 0
if k == 2:
s1 = []
s2 = []
for i in range(n):
if i & 1:
s1.append("A")
s2.append("B")
else:
s2.append("A")
s1.append("B")
a1 = 0
a2 = 0
for i in range(n):
if s[i] != s1[i]:
a1 += 1
if s[i] != s2[i]:
a2 += 1
if a1 > a2:
print(a2)
print("".join(s2))
else:
print(a1)
print("".join(s1))
else:
for i in range(n):
if len(l) == 0:
l.append(s[i])
elif l[-1] == s[i]:
if i != n - 1:
a = l[-1]
b = s[i + 1]
if a == b:
if a == "A":
l.append("B")
else:
l.append("A")
elif a == "A" and b != "B":
l.append("B")
elif a == "A" and b == "B":
l.append("C")
elif a != "B" and b == "A":
l.append("B")
elif a == "B" and b == "A":
l.append("C")
else:
l.append("A")
elif l[-1] == "A":
l.append("B")
else:
l.append("A")
ans += 1
else:
l.append(s[i])
print(ans)
print("".join(l)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | import sys
input = sys.stdin.readline
n, k = map(int, input().split())
s = list(input().rstrip())
al = list("abcdefghijklmnopqrstuvwxyz".upper())[:k]
al = set(al)
if k >= 3:
cnt = 0
for i in range(1, n - 1):
if s[i - 1] == s[i] == s[i + 1]:
s[i] = list(al - {s[i - 1]})[0]
cnt += 1
for i in range(1, n):
if s[i] == s[i - 1]:
if i < n - 1:
s[i] = list(al - {s[i - 1]} - {s[i + 1]})[0]
else:
s[i] = list(al - {s[i - 1]})[0]
cnt += 1
print(cnt)
print("".join(s))
else:
s1 = "AB" * (n // 2) + "A" * (n % 2)
s2 = "BA" * (n // 2) + "B" * (n % 2)
cnt1, cnt2 = 0, 0
for i in range(n):
if s[i] != s1[i]:
cnt1 += 1
if s[i] != s2[i]:
cnt2 += 1
if cnt1 < cnt2:
print(cnt1)
print(s1)
else:
print(cnt2)
print(s2) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | n, k = map(int, input().split())
s = [i for i in input()]
d = {i: chr(65 + i) for i in range(k)}
ct = 0
if k == 2:
a1 = []
a2 = []
for i in range(n):
a1.append(d[i % 2])
a2.append(d[(1 + i) % 2])
ct1 = 0
ct2 = 0
for i in range(n):
if s[i] != a1[i]:
ct1 += 1
if s[i] != a2[i]:
ct2 += 1
if ct2 < ct1:
print(ct2)
print("".join(a2))
else:
print(ct1)
print("".join(a1))
else:
for i in range(1, n):
if s[i] == s[i - 1]:
s[i] = " "
ct += 1
for i in range(1, n - 1):
if s[i] == " ":
for j in range(k):
if s[i - 1] != d[j] and s[i + 1] != d[j]:
s[i] = d[j]
break
if s[-1] == " ":
for j in range(k):
if s[-2] != d[j]:
s[-1] = d[j]
break
print(ct)
print("".join(s)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | n, k = map(int, input().split())
if k == 2:
ab, ba = "AB" * (n // 2) + "A" * (n % 2), "BA" * (n // 2) + "B" * (n % 2)
ab_c, ba_c = 0, 0
old = input().strip()
for i in range(n):
ab_c += old[i] != ab[i]
ba_c += old[i] != ba[i]
if ab_c < ba_c:
print(ab_c)
print(ab)
else:
print(ba_c)
print(ba)
else:
old, req, avail = (
[char for char in input().strip()],
0,
"ABCDEFGHIJKLMNOPQRSTUVWXYZA",
)
for i in range(1, n):
if old[i] == old[i - 1]:
old[i] = avail[(avail.find(old[i - 1]) + 1) % k]
if i != n - 1 and old[i] == old[i + 1]:
old[i] = avail[(avail.find(old[i]) + 1) % k]
req += 1
print(req)
print("".join(old)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | n, k = map(int, input().split())
s = list(input())
if k > 2:
c = set(ord("A") + i for i in range(26))
r = 0
for i in range(1, len(s)):
if s[i - 1] == s[i]:
r += 1
if i + 1 < n:
s[i] = (
"A"
if "A" not in {s[i - 1], s[i + 1]}
else "B" if "B" not in {s[i], s[i + 1]} else "C"
)
else:
s[i] = "A" if s[i - 1] != "A" else "B"
print(r)
print("".join(s))
else:
x, y = "AB" * n, "BA" * n
def check(x, y):
return sum(x[i] != y[i] for i in range(n))
check_1 = check(x, s)
check_2 = check(y, s)
z = [x, y][check_1 > check_2]
print(min(check_1, check_2))
print(z[:n]) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR STRING VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING STRING VAR VAR VAR BIN_OP VAR NUMBER STRING STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER STRING STRING STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR VAR BIN_OP STRING VAR BIN_OP STRING VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | n, k = map(int, input().strip().split())
s = list(input())
options = [chr(ord("A") + i) for i in range(k)]
ans = 0
if k == 2:
ans1 = [("A" if i % 2 == 0 else "B") for i in range(n)]
diff1 = 0
for i in range(n):
if s[i] != ans1[i]:
diff1 += 1
if diff1 < n - diff1:
print(diff1)
print("".join(ans1))
else:
print(n - diff1)
print("B" + "".join(ans1[:-1]))
else:
for i in range(1, n - 1):
if s[i] == s[i - 1]:
ans += 1
for j in options:
if j != s[i - 1] and j != s[i + 1]:
s[i] = j
break
if n > 1 and s[-2] == s[-1]:
ans += 1
s[-1] = options[0] if options[0] != s[-2] else options[1]
print(ans)
print("".join(s)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER STRING STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | n, k = map(int, input().split())
s = input()
temp = []
for i in range(len(s)):
temp.append(s[i])
ans = []
tans = []
for i in range(len(s)):
ans.append(s[i])
tans.append(s[i])
if k == 2:
c1 = 0
count = 0
for i in range(len(ans)):
if i % 2 == 0:
if ans[i] == "B":
ans[i] = "A"
c1 += 1
elif ans[i] == "A":
ans[i] = "B"
c1 += 1
for i in range(len(tans)):
if i % 2 == 0:
if tans[i] == "A":
tans[i] = "B"
count += 1
elif tans[i] == "B":
tans[i] = "A"
count += 1
if count > c1:
print(c1)
print("".join(map(str, ans)))
else:
print(count)
print("".join(map(str, tans)))
else:
for i in range(1, len(ans)):
if ans[i - 1] == ans[i]:
if ans[i] == "A":
if i + 1 < len(ans):
if ans[i + 1] == "B":
ans[i] = "C"
else:
ans[i] = "B"
else:
ans[i] = "B"
elif i + 1 < len(ans):
if ans[i + 1] == "A":
if ans[i] == "B":
ans[i] = "C"
else:
ans[i] = "B"
else:
ans[i] = "A"
else:
ans[i] = "A"
count = 0
for i in range(len(temp)):
if temp[i] != ans[i]:
count += 1
print(count)
print("".join(map(str, ans))) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR STRING IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | n, k = map(int, input().split())
p = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[:k] + "A"
p = {p[i]: p[i + 1] for i in range(k)}
if k == 2:
t = input()
u, v = "AB" * (n // 2) + "A" * (n % 2), "BA" * (n // 2) + "B" * (n % 2)
x, y = sum(int(u[i] != t[i]) for i in range(n)), sum(
int(v[i] != t[i]) for i in range(n)
)
if x < y:
print(x)
print(u)
else:
print(y)
print(v)
else:
t, s = list(input()), 0
for i in range(1, n):
if t[i] == t[i - 1]:
t[i] = p[t[i]]
if i + 1 < n and t[i] == t[i + 1]:
t[i] = p[t[i]]
s += 1
print(s)
print("".join(t)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP STRING VAR STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | from sys import stdin, stdout
nmbr = lambda: int(stdin.readline())
lst = lambda: list(map(int, stdin.readline().split()))
PI = float("inf")
for _ in range(1):
n, k = lst()
s = input()
dp = [[(0) for i in range(k)] for j in range(n)]
mn = smn = 0
for i in range(k):
dp[0][i] = 1
dp[0][ord(s[0]) - 65] = 0
mn = smn = PI
p1 = p2 = -1
for j in range(k):
if dp[0][j] <= mn:
smn = mn
p2 = p1
p1 = j
mn = dp[0][j]
elif dp[0][j] < smn:
smn = dp[0][j]
p2 = j
for i in range(1, n):
mn1 = smn1 = PI
pp1 = pp2 = -1
for j in range(k):
if j == p1:
dp[i][j] = int(ord(s[i]) != j + 65) + dp[i - 1][p2]
else:
dp[i][j] = int(ord(s[i]) != j + 65) + dp[i - 1][p1]
if dp[i][j] <= mn1:
smn1 = mn1
pp2 = pp1
pp1 = j
mn1 = dp[i][j]
elif dp[i][j] < smn1:
smn1 = dp[i][j]
pp2 = j
mn, smn = mn1, smn1
p1, p2 = pp1, pp2
ans = []
cur = mn
prev = -1
for i in range(n - 1, -1, -1):
if prev != -1:
dp[i][prev] = n + 1
for j in range(k):
if dp[i][j] == cur:
pos = j
break
ans += [pos]
if ord(s[i]) != pos + 65:
cur -= 1
prev = pos
print(mn)
for ch in ans[::-1]:
stdout.write(chr(ch + 65)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | from sys import stdin
n, k = map(int, stdin.readline().split())
s = stdin.readline().strip()
cand = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
cnt = 0
if k == 2:
cmps = ("AB" * ((n + 1) // 2))[:n]
for i in range(n):
if s[i] != cmps[i]:
cnt += 1
if cnt < n - cnt:
print(cnt)
print(cmps)
else:
print(n - cnt)
print("".join(("BA" * ((n + 1) // 2))[:n]))
else:
l = 0
r = 1
rs = list(s)
pch = rs[0]
for i in range(1, n):
if rs[i] == pch:
r += 1
else:
d = r - l
if d > 1:
if d % 2:
l += 1
for j in range(l, r, 2):
p = 0
while p < k and (
j > 0
and rs[j - 1] == cand[p]
or j < n - 1
and rs[j + 1] == cand[p]
):
p += 1
rs[j] = cand[p]
cnt += 1
pch = rs[i]
l = r
r = l + 1
d = r - l
if d > 1:
if d % 2:
l += 1
for j in range(l, r, 2):
p = 0
while p < k and (
j > 0 and rs[j - 1] == cand[p] or j < n - 1 and rs[j + 1] == cand[p]
):
p += 1
rs[j] = cand[p]
cnt += 1
print(cnt)
print("".join(rs)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | n, k = map(int, input().split())
s = input()
if k == 2:
sol1 = [s[i] for i in range(n)]
ans1 = 0
for i in range(n):
if i % 2 == 0 and sol1[i] == "B":
ans1 += 1
sol1[i] = "A"
elif i % 2 != 0 and sol1[i] == "A":
ans1 += 1
sol1[i] = "B"
sol2 = [s[i] for i in range(n)]
ans2 = 0
for i in range(n):
if i % 2 == 0 and sol2[i] == "A":
ans2 += 1
sol2[i] = "B"
elif i % 2 != 0 and sol2[i] == "B":
ans2 += 1
sol2[i] = "A"
if ans1 <= ans2:
print(ans1)
print("".join(str(x) for x in sol1))
else:
print(ans2)
print("".join(str(x) for x in sol2))
else:
s = [s[i] for i in range(n)]
ans = 0
for i in range(1, n):
if s[i] == s[i - 1]:
ans += 1
x = chr((ord(s[i - 1]) - 65 + 1) % k + 65)
if i == n - 1 or s[i + 1] != x:
s[i] = x
else:
y = chr((ord(s[i - 1]) - 65 + 1 + 1) % k + 65)
s[i] = y
print(ans)
print("".join(str(x) for x in s)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | def main():
n, k = map(int, input().split())
s = list(input())
if k > 2:
ans = 0
for idx, i in enumerate(s):
if idx == 0:
continue
if s[idx] == s[idx - 1]:
for j in range(k):
c = chr(65 + j)
if s[idx] == c:
continue
elif idx + 1 < n:
if s[idx + 1] == c:
continue
s[idx] = c
ans += 1
break
print(ans)
print("".join(s))
else:
pat1 = "AB" * n
ans1 = 0
for idx, i in enumerate(s):
if i != pat1[idx]:
ans1 += 1
pat2 = "BA" * n
ans2 = 0
for idx, i in enumerate(s):
if i != pat2[idx]:
ans2 += 1
if ans1 <= ans2:
print(ans1)
print(pat1[:n])
else:
print(ans2)
print(pat2[:n])
return
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | n, k = map(int, input().split())
s = input()
if k == 2:
x = "A"
y = "B"
for i in range(1, n):
if x[-1] == "A":
x += "B"
else:
x += "A"
if y[-1] == "A":
y += "B"
else:
y += "A"
p1 = 0
p2 = 0
for i in range(n):
if x[i] != s[i]:
p1 += 1
if y[i] != s[i]:
p2 += 1
if p1 >= p2:
print(p2)
print(y)
else:
print(p1)
print(x)
exit()
ans = s[0]
colors = []
pts = 0
for i in range(k):
colors.append(chr(i + 65))
for i in range(1, n):
x = s[i]
if x != ans[-1]:
ans += x
elif k == 2 or i == n - 1:
for y in colors:
if y != x:
ans += y
pts += 1
break
else:
z = s[i + 1]
for y in colors:
if y != x and y != z:
ans += y
pts += 1
break
print(pts)
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER STRING VAR STRING VAR STRING IF VAR NUMBER STRING VAR STRING VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | n, k = map(int, input().split())
s = input()
b = []
c = []
for j in s:
b.append(j)
c.append(j)
if n == 1:
print(0)
print(b[0])
elif n == 2:
if b[0] == b[1]:
print(1)
print("AB")
else:
print(0)
print("".join(b))
elif k == 2:
b[0] = "A"
j = 1
s = 0
while j < n:
if b[j - 1] == b[j]:
if b[j] == "A":
b[j] = "B"
else:
b[j] = "A"
s += 1
j += 1
p = 0
if c[0] == "B":
s += 1
else:
p += 1
c[0] = "B"
j = 1
while j < n:
if c[j - 1] == c[j]:
if c[j] == "A":
c[j] = "B"
else:
c[j] = "A"
p += 1
j += 1
if p > s:
print(s)
print("".join(b))
else:
print(p)
print("".join(c))
else:
j = 0
s = 0
while j < n - 1:
if j < n - 2 and b[j] == b[j + 1] and b[j] == b[j + 2]:
s += 1
if ord(b[j]) - 64 == k:
b[j + 1] = "A"
else:
b[j + 1] = chr(ord(b[j]) + 1)
j += 2
elif j < n - 2 and b[j] == b[j + 1] and b[j] != b[j + 2]:
s += 1
for p in range(65, 65 + k):
if ord(b[j]) == p or ord(b[j + 2]) == p:
pass
else:
b[j + 1] = chr(p)
break
j += 2
elif j == n - 2 and b[j] == b[j + 1]:
s += 1
if ord(b[j]) - 64 == k:
b[j + 1] = "A"
else:
b[j + 1] = chr(ord(b[j]) + 1)
j += 1
elif b[j] != b[j + 1]:
j += 1
print(s)
print("".join(b)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR NUMBER ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | from sys import stdin
input = stdin.readline
def put():
return map(int, input().split())
n, k = put()
s = [(ord(i) - ord("A")) for i in list(input())]
ans = []
dp = [0] * k
ans.append(dp)
a = b = 0
for i in range(n):
tmp = []
first, second = n + 1, n + 1
for j in range(k):
if dp[j] == a:
y = b
else:
y = a
x = y + (0 if s[i] == j else 1)
if first > x:
second = first
first = x
elif second > x:
second = x
pass
tmp.append(x)
a, b = first, second
dp = tmp
ans.append(tmp)
l = []
x = min(dp)
prev = -1
for i in range(n, 0, -1):
if prev != -1:
ans[i][prev] = n + 1
a = ans[i].index(x)
l.append(chr(a + ord("A")))
if s[i - 1] != a:
x -= 1
prev = a
print(min(dp))
y = "".join(l[::-1])
print(y) | ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR NUMBER EXPR FUNC_CALL VAR VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | n, k = map(int, input().split())
s = list(input())
l = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")[:k]
ll = set(l)
if k >= 3:
cnt = 0
for i in range(1, n):
if s[i] == s[i - 1]:
cnt += 1
if i + 1 < n:
s[i] = list(ll - {s[i - 1]} - {s[i + 1]})[0]
else:
s[i] = list(ll - {s[i - 1]})[0]
print(cnt)
print("".join(s))
else:
x = ("AB" * n)[:n]
y = ("BA" * n)[:n]
cnt1, cnt2 = 0, 0
for i in range(n):
if x[i] != s[i]:
cnt1 += 1
if y[i] != s[i]:
cnt2 += 1
if cnt1 >= cnt2:
print(cnt2)
print(y)
else:
print(cnt1)
print(x) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP STRING VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | n, k = map(int, input().split())
s = input()
a = s
s = []
for i in range(len(a)):
s.append(a[i])
prev = 0
alphs = []
ans = 0
for i in range(k):
alphs.append(chr(ord("A") + i))
if k == 2:
f = []
sec = []
t = 0
for i in range(n):
f.append(chr(ord("A") + t))
sec.append(chr(ord("A") + (t + 1) % 2))
t ^= 1
ans1 = 0
ans2 = 0
for i in range(n):
if s[i] != f[i]:
ans1 += 1
if s[i] != sec[i]:
ans2 += 1
if ans1 <= ans2:
print(ans1)
for i in f:
print(i, end="")
else:
print(ans2)
for i in sec:
print(i, end="")
else:
for i in range(1, n):
if s[i] != s[prev]:
if i - prev > 1:
if (i - prev) % 2:
ans += (i - prev) // 2
t = 0
put = ""
for j in alphs:
if j != s[prev]:
put = j
break
for j in range(prev, i):
if t % 2:
s[j] = put
t += 1
else:
end = ""
other = ""
for j in alphs:
if j != s[prev] and j != s[i]:
end = j
break
for j in alphs:
if j != s[prev] and j != end:
other = j
break
s[i - 1] = end
ans += 1
t = 0
for j in range(i - 2, prev - 1, -1):
if t % 2:
s[j] = other
ans += 1
t += 1
prev = i
i = n - 1
for i in range(n - 1, -1, -1):
if s[i] != s[n - 1]:
prev = i + 1
break
i = n - 1
if i - prev > 0:
if (i - prev + 1) % 2:
ans += (i - prev + 1) // 2
t = 0
put = ""
for j in alphs:
if j != s[i]:
put = j
break
for j in range(prev, i + 1):
if t % 2:
s[j] = put
t += 1
else:
other = ""
for j in alphs:
if j != s[prev]:
other = j
break
t = 0
for j in range(i, prev - 1, -1):
if t % 2 == 0:
s[j] = other
ans += 1
t += 1
print(ans)
for i in s:
print(i, end="") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | n, k = map(int, input().split())
s = input()
l = [chr(65 + i) for i in range(k)]
p = 0
if k == 2:
q = "AB" * (n // 2) + "A" * (n % 2)
w = "BA" * (n // 2) + "B" * (n % 2)
o = 0
for i in range(n):
if s[i] != q[i]:
p += 1
if s[i] != w[i]:
o += 1
if p > o:
print(o, "\n", w)
else:
print(p, "\n", q)
else:
s = list(s + "0")
for i in range(1, n):
if s[i] == s[i - 1]:
k = s[i]
for j in l:
if s[i - 1] != j and s[i + 1] != j:
s[i] = j
p += 1
break
print(p, "\n", "".join(s[:-1])) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR EXPR FUNC_CALL VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FUNC_CALL STRING VAR NUMBER |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | n, k = map(int, input().split())
a = list(input().rstrip())
if k > 2:
ans, s = 0, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[:k]
for i in range(n - 1):
if a[i] == a[i + 1]:
for j in s:
if j != a[i] and (i + 2 < n and j != a[i + 2] or i == n - 2):
a[i + 1] = j
ans += 1
break
print(ans)
print("".join(a))
else:
s1, s2, x, y = (
"AB" * (n // 2) + "A" * (n % 2),
"BA" * (n // 2) + "B" * (n % 2),
0,
0,
)
for i in range(n):
if a[i] != s1[i]:
x += 1
if a[i] != s2[i]:
y += 1
if x > y:
print(y)
print(s2)
else:
print(x)
print(s1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER STRING VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | n, k = list(map(int, input().split()))
a = [i for i in input()]
b = 0
if k == 2:
for i in range(n):
if a[i] != "AB"[i % 2]:
b += 1
if b < n - b:
print(b)
print("AB" * (n // 2) + "A" * (n % 2))
else:
print(n - b)
print("BA" * (n // 2) + "B" * (n % 2))
else:
for i in range(1, n):
if a[i] == a[i - 1]:
for j in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[:k]:
if j != a[i - 1] and (i == n - 1 or j != a[i + 1]):
a[i] = j
b += 1
break
print(b)
print("".join(a)) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER FOR VAR STRING VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | n, m = map(int, input().split())
for k in range(33):
m = min(m, abs(n ** (32 - k) - m))
print(["NO", "YES"][m == 0]) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR LIST STRING STRING VAR NUMBER |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | w, m = map(int, input().split())
A = []
while m:
A.append(m % w)
m //= w
sz = len(A)
A.append(0)
for i in range(sz):
x = A[i]
if x != 0 and x != 1 and x != w - 1 and x != w:
print("NO")
exit()
if x == w - 1 or x == w:
A[i + 1] += 1
print("YES") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | M, N = map(int, input().split())
used = {}
def check(K):
while K:
KP = 0
while K:
if K % M == 0:
K /= M
elif K % M == 1 and KP not in used:
K = (K - 1) / M
else:
return False
KP += 1
return True
while N:
if check(N):
break
P = 0
while M**P < N:
P += 1
high = M**P
low = M ** (P - 1) if P else 0
if P in used and P - 1 in used:
print("NO")
exit()
elif P in used:
used[P - 1] = True
N = N - low
elif P - 1 in used:
used[P] = True
N = high - N
elif high - N < N:
used[P] = True
N = high - N
else:
used[P - 1] = True
N = N - low
print("YES") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF WHILE VAR ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN NUMBER VAR NUMBER RETURN NUMBER WHILE VAR IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | n, m = map(int, input().split())
a = 0
d = True
for i in range(100):
if d and n > 0:
if m % n ** (a + 1) == n**a:
m -= n**a
a += 1
elif m % n ** (a + 1) == 0:
a += 1
elif (m + n**a) % n ** (a + 1) == 0:
m += n**a
a += 1
else:
d = False
print("NO")
if d:
print("YES") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | w, m = map(int, input().split())
def solve(m, curw):
cursum = 0
if m == 0:
return 1
for i in range(curw):
cursum += w**i
if cursum == m:
return 1
if cursum > m or w**i == m:
return solve(abs(w**i - m), i)
return 0
print("YES" if solve(m, 100) else "NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER STRING STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | R = lambda: list(map(int, input().split()))
w, m = R()
while m:
k = m % w
if k == 1:
m -= 1
elif k == w - 1:
m += 1
elif k == 0:
pass
else:
print("NO")
exit(0)
m //= w
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | def f(m, t):
w = 0
nonlocal n
while n**w * n < m:
w += 1
if n**w * n == m or m == 1 and t != 0:
return True
else:
s1 = m - n**w
j = False
k = False
if w < t:
j = f(abs(s1), w)
if w + 1 < t:
k = f(abs(n**w * n - m), w + 1)
if k == True or j == True:
return True
else:
return False
n, m = list(map(int, input().split()))
if f(m, 101):
print("YES")
else:
print("NO") | FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | w, m = map(int, input().split())
o = []
while m > 0:
o.append(m % w)
m //= w
o.append(0)
f = 0
for i in range(len(o) - 1):
o[i + 1] += o[i] // w
o[i] %= w
if o[i] == 0:
continue
if o[i] == 1:
continue
elif o[i] < w - 1:
f = 1
else:
o[i + 1] += 1
print("YES" if f == 0 else "NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | __author__ = "lenovo-pc"
w, m = map(int, input().split(" "))
ans = True
while m:
k = m % w
if k == 1:
m -= 1
elif k == w - 1:
m += 1
elif k == 0:
pass
else:
ans = False
m //= w
if ans:
print("YES")
else:
print("NO") | ASSIGN VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | __author__ = "artbataev"
def tryWeight(w, m, num):
if num < 1:
return False
if m == 1:
return True
if m % w == 0:
return tryWeight(w, m // w, num - 1)
if (m + 1) % w == 0:
return tryWeight(w, (m + 1) // w, num - 1)
if (m - 1) % w == 0:
return tryWeight(w, (m - 1) // w, num - 1)
return False
w, m = list(map(int, input().split()))
if tryWeight(w, m, 101):
print("YES")
else:
print("NO") | ASSIGN VAR STRING FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | a, b = list(map(int, input().split(" ")))
if a == 2 or a == 3:
print("YES")
return
else:
while b != 0:
if b % a == 0:
b //= a
elif b % a == 1:
b -= 1
b //= a
elif b % a == a - 1:
b += 1
b //= a
else:
print("NO")
return
print("YES") | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | R = lambda: map(int, input().split())
w, m = R()
bs = [1]
while bs[-1] < 10**10:
bs.append(bs[-1] * w)
arr = [0] * len(bs)
for i in range(len(bs) - 1, -1, -1):
if m >= bs[i]:
arr[i] = m // bs[i]
m %= bs[i]
for i in range(len(bs)):
if 1 < arr[i] < w - 1:
print("NO")
exit(0)
if arr[i] > 1 and (arr[i] == w - 1 or arr[i] == w):
arr[i + 1] += 1
print("YES") | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER WHILE VAR NUMBER BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | w, m = map(int, input().split())
def b(x, base):
digits = []
while x:
digits.append(x % base)
x = int(x / base)
return digits
digs = b(m, w)
f = -1
s = 0
while True:
for i in range(s, len(digs)):
if digs[i] not in {0, 1}:
if digs[i] != w - 1:
print("NO")
exit()
f = i
break
if f != -1 and f != len(digs) - 1:
m += w**f
digs = b(m, w)
s = f
f = -1
else:
break
print("YES") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | def main():
w, m = map(int, input().split())
ww = 1
while ww < m:
ww *= w
while m and ww:
m = min(m, m - ww, m + ww, key=abs)
ww //= w
print(("YES", "NO")[bool(m)])
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | w, m = list(map(int, input().split()))
for u in (w ** (100 - i) for i in range(101)):
m = min(m, abs(m - u))
print("YES" if m == 0 else "NO") | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | def main():
w, m = map(int, input().split())
while m > 1:
c = m % w
if c not in {0, 1, w - 1}:
return print("NO")
m //= w
if c == w - 1:
m += 1
print("YES")
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR STRING VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | w, m = map(int, input().split())
arr = [0] * 103
i = 0
while m != 0:
arr[i] = m % w
m //= w
i += 1
for j in range(i):
if arr[j] == 1 or arr[j] == 0:
pass
elif arr[j] % w == 0:
arr[j + 1] += 1
elif arr[j] == w - 1:
arr[j + 1] += 1
else:
print("NO")
break
else:
print("YES") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | def rec(remain, prev_index):
if remain == 0:
return True
if prev_index == 0:
return False
index = -1
for i in range(prev_index):
if remain > 0 and w ** (i + 1) - remain >= 0:
index = i
break
elif remain < 0 and w ** (i + 1) + remain >= 0:
index = i
break
if index == -1:
return False
if remain > 0:
w1 = w**index
w2 = w1 * w
else:
w1 = -(w**index)
w2 = w1 * w
if index + 1 == prev_index:
return rec(remain - w1, index)
return rec(remain - w1, index) or rec(remain - w2, index + 1)
w, m = [int(a) for a in input().split()]
if rec(m, 101):
print("YES")
else:
print("NO") | FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | n, m = map(int, input().split())
flag = 0
while m > 0:
if (m - 1) % n == 0:
m -= 1
elif (m + 1) % n == 0:
m += 1
if m % n != 0:
flag = -1
break
m //= n
if flag == 0:
print("YES")
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | x = [0] * 101
ww, m = map(int, input().split())
if ww == 2:
print("YES")
exit()
w = ww
c = 0
while m:
x[c] = m % w
m //= w
c += 1
an = 1
w = ww
vis = [0] * 101
for i in range(101):
if x[i] == w - 1:
vis[i] = 1
x[i] = 0
if i + 1 > 100:
an = 0
break
else:
x[i + 1] += 1
elif x[i] == w:
x[i] = 0
x[i + 1] += 1
for i in range(101):
if x[i] > 1:
an = 0
break
elif x[i] == 1 and vis[i] != 0:
an = 0
break
print("YES" if an else "NO") | ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | w, n = map(int, input().split(" "))
r = 1
if w <= 3:
n = 0
while n:
if not n % w in {0, 1, w - 1}:
r = 0
if n % w == w - 1:
n = n // w + 1
else:
n = n // w
if r:
print("YES")
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | base, num = map(int, input().split())
if base == 3 or base == 2:
print("YES")
quit()
newNum = []
while num > 0:
newNum = [num % base] + newNum
num //= base
newNum = [0] + newNum
for i in range(len(newNum) - 1, 0, -1):
if newNum[i] == base or newNum[i] == base - 1:
newNum[i - 1] += 1
continue
if newNum[i] > 1:
print("NO")
quit()
print("YES") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | import sys
def seek(x, terms, sums, index):
if x == 0 or sums[index] == x:
print("YES")
sys.exit()
if sums[index] < abs(x):
return
if x > 0:
seek(x - terms[index], terms, sums, index - 1)
else:
seek(x + terms[index], terms, sums, index - 1)
seek(x, terms, sums, index - 1)
base, x = map(int, input().split())
terms = [1]
sums = [1]
while terms[-1] < x:
terms.append(base * terms[-1])
sums.append(sums[-1] + terms[-1])
seek(x, terms, sums, len(terms) - 1)
print("NO") | IMPORT FUNC_DEF IF VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR VAR FUNC_CALL VAR VAR RETURN IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | __author__ = "victormion"
w, m = input().strip().split()
w = int(w)
m = int(m)
def solve(w, m):
return (
w <= 3 or m == 1 or trySolve(w, m - 1) or trySolve(w, m) or trySolve(w, m + 1)
)
def trySolve(w, m):
return m % w == 0 and solve(w, m // w)
if solve(w, m):
print("YES")
else:
print("NO") | ASSIGN VAR STRING ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | def VI():
return list(map(int, input().split()))
def Y():
print("YES")
def run(w, m):
if w <= 3:
Y()
return
mm = m
for i in range(100):
if m % w == 0:
m //= w
elif m % w == 1:
m = (m - 1) // w
elif m % w == w - 1:
m = (m + 1) // w
else:
break
if m == 0:
Y()
return
print("NO")
def main(info=0):
w, m = VI()
run(w, m)
def __starting_point():
main()
__starting_point() | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR RETURN ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR STRING FUNC_DEF NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | w, m = map(int, input().split())
def f(m, w):
while m != 0:
x = (m + 1) % w
if x == 0:
m = (m + 1) // w
elif x < 3:
m = m // w
else:
return False
return True
if w == 2:
print("YES")
elif f(m, w):
print("YES")
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | w, x = map(int, input().split())
a = []
cur = w**50
while cur:
if x >= cur:
a.append(x // cur)
x %= cur
else:
a.append(0)
cur //= w
for i in range(50, 1, -1):
if a[i] > 1:
a[i] -= w
a[i - 1] += 1
if a[i] not in {-1, 0, 1}:
print("NO")
exit(0)
print("YES") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | M, N = map(int, input().split())
used = {}
P = 0
while M**P < N:
P += 1
for p in range(P, -1, -1):
k = M**p
if abs(k - N) < N:
N = abs(k - N)
if N == 0:
print("YES")
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | base, massa = map(int, input().split())
for i in range(100, -1, -1):
if abs(massa - base**i) < massa:
massa = abs(massa - base**i)
if massa == 0:
print("YES")
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | W, M = input().split(" ")
W = int(W)
M = int(M)
if W == 2 or W == 3:
print("YES")
else:
N = 16
A = [0] * (N + 1)
A[0] = 1
for I in range(1, N):
A[I] = A[I - 1] * W
if A[I] > 10000000001000000000:
N = I
break
S = set()
ok = False
for msk in range(1 << N):
curr = 0
for I in range(N):
if msk & 1 << I > 0:
curr += A[I]
if curr == M or curr - M in S:
ok = True
break
S.add(curr)
if ok:
print("YES")
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | w, m = tuple(map(int, input().split()))
f = True
while m > 0:
if m % w == 0:
m //= w
elif m % w == 1:
m //= w
elif m % w == w - 1:
m = m // w + 1
else:
f = False
break
print("YES" if f else "NO") | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | w, m = map(int, input().split())
s = 0
p = 1
for i in range(101):
s += p
p *= w
m += s
bad = False
while m > 0:
if m % w > 2:
print("NO")
exit()
m //= w
print("YES") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | w, m = map(int, input().split())
f = 0
ff = 0
while m != 0:
i = 0
ma = 0
while w**i < m:
ma += w**i
i += 1
if w**i - ma <= m:
ff = 1
m = w**i - m
elif ma >= m:
ff = 2
m = m - w ** (i - 1)
else:
f = 1
print("NO")
break
if not f:
print("YES") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | w, m = [int(i) for i in input().split()]
if w == 0:
if m <= 101:
print("YES")
else:
print("NO")
exit(0)
a = []
m1 = m
while m1 > 0:
a.append(m1 % w)
m1 //= w
if len(a) > 101:
print("NO")
exit(0)
s = True
for i in range(len(a)):
if a[i] != 0 and a[i] != 1:
s = False
if s:
print("YES")
exit(0)
s = True
for i in range(len(a) - 1):
if a[i] != 0 and a[i] != 1:
a[i] -= w
a[i + 1] += 1
if a[i] != -1 and a[i] != 0:
print("NO")
exit(0)
if a[-1] > 1:
if len(a) > 100:
print("NO")
exit(0)
a[-1] -= w
if a[-1] != 0 and a[-1] != -1:
print("NO")
exit(0)
print("YES") | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | base, need = map(int, input().split())
if base == 2:
print("YES")
else:
digits = []
while need:
digits.append(need % base)
need //= base
res_found = False
for i in range(len(digits)):
if digits[i] == base - 1:
digits[i] = 0
try:
digits[i + 1] += 1
j = i + 1
while digits[j] == base:
digits[j] = 0
try:
digits[j + 1] += 1
except:
digits.append(1)
j += 1
except:
digits.append(1)
elif digits[i] > 1:
print("NO")
res_found = True
break
if not res_found:
print("YES") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | def can_weigh(w, m):
while m > 0:
if (m - 1) % w == 0:
m -= 1
elif (m + 1) % w == 0:
m += 1
elif m % w != 0:
return "NO"
m //= w
return "YES"
w, m = map(int, input().split())
result = can_weigh(w, m)
print(result) | FUNC_DEF WHILE VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN STRING VAR VAR RETURN STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
-----Input-----
The first line contains two integers w, m (2 β€ w β€ 10^9, 1 β€ m β€ 10^9) β the number defining the masses of the weights and the mass of the item.
-----Output-----
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
-----Examples-----
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
-----Note-----
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | w, m = map(int, input().split())
mass = []
while m > 0:
mass.append(m % w)
m //= w
mass.append(0)
for i in range(len(mass) - 1):
if mass[i] >= w - 1:
mass[i + 1] += 1
mass[i] = 0
ok = 1
for i in range(len(mass)):
if mass[i] > 1:
ok = 0
print("NO" if ok == 0 and w != 2 else "YES") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING STRING |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.