description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | shifts = [(2**p) for p in range(62, -1, -1)]
def solve(x, y, l, r):
if l == 0 and r >= 2 * max(x, y):
if x * y == 0:
return 0
return x | y
result = l
current = l
best = (x & current) * (y & current)
while True:
last0 = ~current
last0 = last0 & -last0
current += last0
if current > r:
current -= last0 - 1
if current > r:
break
val = (x & current) * (y & current)
if val > best:
best = val
result = current
for shift in shifts:
tmp = result ^ shift
if tmp > result or tmp < l or (tmp & x) * (tmp & y) != best:
continue
result = tmp
return result
tests = int(input())
for t in range(tests):
x, y, l, r = map(int, input().split())
print(solve(x, y, l, r)) | ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR WHILE NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | def rangingl(lmin, ans):
global l
if lmin & lmin - 1 == 0:
x = lmin | ans
return x
else:
y = rangingl(lmin & lmin - 1, ans)
if y >= l:
return y
return lmin | ans
t = int(input())
for test in range(t):
x, y, l, r = map(int, input().split())
p = x | y
p1 = bin(p)
fmax = x * y
if x == 0 or y == 0:
print(l)
elif p <= r and p >= l:
print(p)
else:
fmax = (x & r) * (y & r)
z = r
ans = r
while z:
temp = z - 1
if temp < l:
break
if (x & temp) * (y & temp) >= fmax:
fmax = (x & temp) * (y & temp)
ans = temp
z = z & z - 1
if (x & l) * (y & l) >= fmax:
fmax = (x & l) * (y & l)
ans = l
s = ans & p
if s >= l:
print(s)
else:
x = rangingl(l, s)
print(x) | FUNC_DEF IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR RETURN VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | def initial(x, y, l, r):
ton = l
isan = 0
for i in reversed(range(41)):
flag = 1 << i & r
if flag >> i == 0:
continue
mtan = (1 << i) - 1 | r - (1 << i)
use = (x & mtan) * (y & mtan)
if use > isan and mtan >= l:
isan = use
ton = mtan
here = (x & r) * (y & r)
if here > isan:
ton = r
return ton
def final(x, y, l, r, ton):
left = 0
temp = x | y
for i in reversed(range(41)):
flag = 1 << i
if (temp & flag) >> i == 1:
continue
if ton - flag < l:
continue
if (ton & flag) >> i == 0:
continue
ton -= flag
for i in reversed(range(41)):
left = (x & ton) * (y * ton)
if left > r or left < l:
left = 0
else:
continue
return ton
for _ in range(int(input())):
x, y, l, r = map(int, input().split())
if x != 0 and y != 0:
ton = initial(x, y, l, r)
ton = final(x, y, l, r, ton)
print(ton)
else:
print(l) | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | for _ in range(int(input())):
x, y, l, r = list(map(int, input().split()))
if l == r:
print(l)
continue
xb = bin(x).replace("0b", "")
yb = bin(y).replace("0b", "")
rb = bin(r).replace("0b", "")
lb = bin(l).replace("0b", "")
t_1, t_2, poss = "", "", []
maxLen = max(len(xb), len(yb), len(rb), len(lb))
xb = "0" * (maxLen - len(xb)) + xb
yb = "0" * (maxLen - len(yb)) + yb
rb = "0" * (maxLen - len(rb)) + rb
lb = "0" * (maxLen - len(lb)) + lb
right = ""
for i in range(maxLen):
if xb[i] == "1" or yb[i] == "1":
right = right + "1"
else:
right = right + "0"
for i in range(maxLen):
if lb[i] != rb[i]:
ChangeInd = i
break
j = ChangeInd + 1
checkZgreaterThanL = False
t_1 = rb[:ChangeInd]
tempRor = ""
while j < maxLen:
if checkZgreaterThanL == False:
if lb[j] == "1":
tempRor += "1"
elif xb[j] == "1" or yb[j] == "1":
tempRor += "1"
checkZgreaterThanL = True
else:
tempRor += "0"
elif xb[j] == "1" or yb[j] == "1":
tempRor += "1"
else:
tempRor += "0"
j += 1
t_2 = t_1 + "0" + tempRor
poss.append(t_2)
t_1 = t_1 + "1"
for i in range(ChangeInd + 1, maxLen - 1):
Ror = ""
if rb[i] == "1":
if i + 1 < maxLen:
Ror = right[i + 1 :]
t_2 = t_1 + "0" + Ror
poss.append(t_2)
t_1 += "1"
else:
t_1 += "0"
poss.append(rb[:-1] + "0")
poss.append(rb)
poss.append(lb)
maxVal = -1
z = r + 1
pOnce = {}
for i in poss:
pOnce[int(i, 2)] = True
for i in pOnce:
fVal = (x & i) * (y & i)
if fVal > maxVal:
z = i
maxVal = fVal
elif fVal == maxVal:
z = min(i, z)
maxVal = fVal
if maxVal == 0:
z = l
print(z) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR VAR VAR STRING STRING LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR STRING WHILE VAR VAR IF VAR NUMBER IF VAR VAR STRING VAR STRING IF VAR VAR STRING VAR VAR STRING VAR STRING ASSIGN VAR NUMBER VAR STRING IF VAR VAR STRING VAR VAR STRING VAR STRING VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR STRING IF VAR VAR STRING IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR VAR VAR STRING VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | def f(q):
fi = []
ch = False
for i in range(q, 0, -1):
if b[-i] == a[-i]:
fi.append(a[-i])
elif b[-i] == "1":
if ch:
fi.append("0")
else:
fi.append("1")
elif b[-i] == "0":
ch = True
fi.append("1")
return "".join(fi)
for _ in range(int(input())):
x, y, l, r = map(int, input().split())
z = x | y
a = bin(z)[2:]
b = bin(l)[2:]
c = bin(r)[2:]
q = len(a)
if x == 0 or y == 0:
print(l)
continue
if r - l <= 1:
if (x & l) * (y & l) >= (x & r) * (y & r):
print(l)
else:
print(r)
continue
elif l <= z <= r:
print(z)
continue
elif l > z:
t = int(b[:-q] + a, 2)
t1 = int(b[-q:], 2)
if t1 <= z and r >= t:
print(t)
continue
elif t1 > z:
t = int(b[:-q] + f(q), 2)
if t <= r:
print(t)
continue
b = b.rjust(q, "0")
c = c.rjust(q, "0")
for i in range(q, 1, -1):
if b[-i] != c[-i]:
break
fi = b[: -i + 1] + f(i - 1)
t = int(fi, 2)
m = (x & t) * (y & t)
if a[-i] == "0" and m != 0:
print(t)
else:
for j in range(i - 1, 0, -1):
if a[-j] == "0" and c[-j] == "1":
t1 = int(c[:-j] + a[-j:], 2)
m1 = (x & t1) * (y & t1)
if m1 > m:
m = m1
t = t1
break
elif a[-j] == "1" and c[-j] == "1" and j != 1:
t1 = int(c[:-j] + "0" + a[-j + 1 :], 2)
m1 = (x & t1) * (y & t1)
if m1 > m:
m = m1
t = t1
else:
continue
if (x & r) * (y & r) > m:
print(r)
elif m == 0:
print(l)
else:
print(t) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | from sys import stdin
def solve(X, Y, L, R):
mx, mxz = -1, -1
def search(i, Z, freeL, freeR):
nonlocal mx, mxz
if i < 0:
res = (X & Z) * (Y & Z)
if res > mx or res == mx and Z < mxz:
mx, mxz = res, Z
return
if freeL and freeR:
Z = Z << i + 1 | (X | Y) & (1 << i + 1) - 1
res = (X & Z) * (Y & Z)
if res > mx or res == mx and Z < mxz:
mx, mxz = res, Z
return
x, y, l, r = X >> i & 1, Y >> i & 1, L >> i & 1, R >> i & 1
Z <<= 1
if freeL:
if not r:
search(i - 1, Z, True, False)
else:
search(i - 1, Z + 1, True, False)
search(i - 1, Z, True, True)
return
if freeR:
if l:
search(i - 1, Z + 1, False, True)
else:
search(i - 1, Z + 1, True, True)
search(i - 1, Z, False, True)
return
if l == r:
Z += r
search(i - 1, Z, False, False)
return
assert l < r
search(i - 1, Z, False, True)
search(i - 1, Z + 1, True, False)
search(42, 0, False, False)
return mxz
def main():
from sys import stdin
T = int(stdin.readline().strip())
for t in range(T):
X, Y, L, R = list(map(int, stdin.readline().strip().split()))
out = solve(X, Y, L, R)
print(out)
main() | FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN IF VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | def solve(ind, restriction, ans):
sol = []
flag = 0
for i in range(ind, m):
if x[i] == "1" or y[i] == "1":
if not restriction:
ans += "1"
elif r[i] == "0":
ans += "0"
else:
sol.extend(solve(i + 1, 1, ans + "1"))
sol.extend(solve(i + 1, 0, ans + "0"))
flag = 1
break
else:
if l[0 : len(ans)] == ans and l[i] == "1":
ans += "1"
continue
ans += "0"
if r[i] == "1":
restriction = 0
if not flag:
sol.append(ans)
return sol
for nt in range(int(input())):
x, y, l, r = map(int, input().split())
if True:
if True:
if True:
if x == 0 or y == 0:
print(l)
continue
x, y = bin(x)[2:], bin(y)[2:]
l, r = bin(l)[2:], bin(r)[2:]
m = max(len(x), len(y), len(l), len(r))
x = "0" * (m - len(x)) + x
y = "0" * (m - len(y)) + y
l = "0" * (m - len(l)) + l
r = "0" * (m - len(r)) + r
answer = solve(0, 1, "")
ans = 0
num = int(l, 2)
for i in answer:
if (int(x, 2) & int(i, 2)) * (int(y, 2) & int(i, 2)) > ans and int(
i, 2
) >= int(l, 2):
ans = (int(x, 2) & int(i, 2)) * (int(y, 2) & int(i, 2))
num = int(i, 2)
elif (int(x, 2) & int(i, 2)) * (
int(y, 2) & int(i, 2)
) == ans and int(i, 2) >= int(l, 2):
num = min(int(i, 2), num)
print(num) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR VAR STRING IF VAR VAR STRING VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR STRING VAR STRING VAR STRING IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF NUMBER IF NUMBER IF NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | from sys import stdin
def decimalToBinary(n):
return bin(n).replace("0b", "")
t = int(stdin.readline())
for test in range(t):
X, Y, L, R = map(int, stdin.readline().split())
maxi = str(decimalToBinary(X | Y))
binL = str(decimalToBinary(L))
binR = str(decimalToBinary(R))
maxLen = max(len(maxi), len(binL), len(binR))
maxi0 = maxi.zfill(maxLen)
binL0 = binL.zfill(maxLen)
binR0 = binR.zfill(maxLen)
maxi0Copy = maxi0
binL0Copy = binL0
binR0Copy = binR0
listR = []
if X == 0 or Y == 0:
print(L)
continue
elif L == 0:
for i in range(len(binR0)):
if binR0Copy[i] == "1":
binary = "0b" + binR0[0:i] + "0" + maxi0[i + 1 :]
if L <= int(binary, 2) <= R:
listR.append(int(binary, 2))
elif L > 0 and R > 0:
for i in range(len(binR0)):
if binR0Copy[i] == "1":
binary = "0b" + binR0[0:i] + "0" + maxi0[i + 1 :]
if L <= int(binary, 2) <= R:
listR.append(int(binary, 2))
for i in range(len(binL0)):
if binL0Copy[i] == "1":
binary = "0b" + binL0[0 : i + 1] + maxi0[i + 1 :]
if L <= int(binary, 2) <= R:
listR.append(int(binary, 2))
if L not in listR:
listR.append(L)
if R not in listR:
listR.append(R)
if L <= X | Y <= R and X | Y not in listR:
listR.append(X | Y)
ele = {}
for i in range(len(listR)):
key = (X & listR[i]) * (Y & listR[i])
if key in ele:
ele[key].append(listR[i])
else:
ele[key] = [listR[i]]
maximum = 0
for i in ele:
if i > maximum:
maximum = i
print(min(ele[maximum])) | FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP STRING VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | def chandf(x, y, l, r):
a = (x & l) * (y & l)
id = l
Z = x | y
for i in range(64):
if r & 1 << i:
k = r ^ 1 << i | (1 << i) - 1
if k < l:
continue
for j in range(i - 1, -1, -1):
if Z & 1 << j == 0:
if k ^ 1 << j >= l:
k = k ^ 1 << j
temp = (k & x) * (k & y)
if temp > a:
a = temp
id = k
elif temp == a:
id = min(id, k)
if (x & r) * (y & r) > a:
id = r
print(id)
for _ in range(int(input())):
x, y, l, r = map(int, input().split())
chandf(x, y, l, r) | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | mxB = 1 << 40
def f(x, y, z):
return (z & x) * (z & y)
def lcomp(a, b):
return a & -b - 1
def find(x, y, l, r, b=mxB):
z = x | y
w = x & y
ans = z
lans = lcomp(z, l)
while b > 0 and r & b == 0:
b >>= 1
z = 0 if b == 0 else z & 2 * b - 1
rb = b
pv = 0
while b > 0 and (r < z or z < l):
if z < l and l & b:
z |= b
if z & b:
if r < z and r & b == 0:
z -= b
if r & b and (l & b == 0 or z - b > l):
v = f(x, y, z - b)
lz = lcomp(z - b, l)
if v > pv or v == pv and lz < lans:
ans = z - b
lans = lz
pv = v
b >>= 1
v = f(x, y, z)
lz = lcomp(z, l)
if v > pv or v == pv and lz < lans:
ans = z
pv = v
b = rb
while b > 0 and ans < l:
if l & b:
ans |= b
b >>= 1
if pv == 0:
ans = l
return ans
for t in range(int(input().strip())):
x, y, l, r = tuple(map(int, input().strip().split()))
ans = 0
z = x | y
ans = find(x, y, l, r)
print(ans) | ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | a = int(input())
for i in range(a):
x, y, l, r = map(int, input().split())
if x == 0 or y == 0:
print(l)
continue
ans = x | y
if l == 0 and r >= ans:
print(ans)
continue
arr = []
prev = 0
if l == r:
print(l)
continue
for j in range(43):
k = 1 << j
if ans & k != 0:
arr.append(prev + k)
else:
arr.append(prev)
prev = arr[j]
z = 0
k = 0
for j in range(42, -1, -1):
m = 1 << j
k = j
if l & m == r & m:
if r & m != 0:
z += m
else:
break
p2 = l
p1 = r
ans1 = (x & p1) * (y & p1)
ans2 = (x & p2) * (y & p2)
z1 = z + (1 << k)
z2 = z
k -= 1
for j in range(k, 0, -1):
m = 1 << j
add = arr[j - 1]
if r & m != 0:
p = z1 + add
if (x & p) * (y & p) > ans1 or ans1 == (x & p) * (y & p) and p < p1:
ans1 = (x & p) * (y & p)
p1 = p
z1 += m
if l & m == 0 and ans & m != 0:
p = z2 + m + add
if (x & p) * (y & p) > ans2 or (x & p) * (y & p) == ans2 and p < p2:
ans2 = (x & p) * (y & p)
p2 = p
elif l & m != 0:
z2 += m
add = 0
m = 1
if r & m != 0:
p = z1 + add
if (x & p) * (y & p) > ans1 or ans1 == (x & p) * (y & p) and p < p1:
ans1 = (x & p) * (y & p)
p1 = p
if l & m == 0 and ans & 1 != 0:
p = z2 + m + add
if (x & p) * (y & p) > ans2 or (x & p) * (y & p) == ans2 and p < p2:
ans2 = (x & p) * (y & p)
p2 = p
if p1 == p2:
print(p1)
elif ans1 > ans2:
print(p1)
elif ans1 < ans2:
print(p2)
else:
print(min(p1, p2)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | def duvidha(binary, ini):
for i in range(ini, len(binary)):
a1 = 2 ** (len(binary) - i - 1)
if (
int(binary, 2) + a1 <= l
or i == len(binary) - 1
and (binary_x[i] == "1" or binary_y[i] == "1")
and int(binary, 2) + a1 <= r
):
binary = binary[:i] + "1" + binary[i + 1 :]
elif binary_x[i] == "1" and binary_y[i] == "1":
if int(binary, 2) + a1 <= r:
binary = binary[:i] + "1" + binary[i + 1 :]
elif binary_x[i] == "1" or binary_y[i] == "1":
if int(binary, 2) + a1 <= r:
binary1_x = binary_x[i + 1 :]
binary1_y = binary_y[i + 1 :]
a = int(binary1_x, 2) | int(binary1_y, 2)
if int(binary, 2) + a1 + a <= r:
binary = binary[:i] + "1" + binary[i + 1 :]
binary = int(binary, 2) | a
binary = bin(binary)
binary = binary[2:]
break
else:
b = int(binary1_x, 2) | int(binary1_y, 2)
b = b | int(binary, 2)
binary = binary[:i] + "1" + binary[i + 1 :]
binary = duvidha(binary, i + 1)
binary1 = bin(b)
binary1 = binary1[2:]
binary1 = "0" * (len(binary_x) - len(binary1)) + binary1
if (int(binary1, 2) & x) * (int(binary1, 2) & y) >= (
int(binary, 2) & x
) * (int(binary, 2) & y):
binary = binary1
break
return binary
def less_than(binary, l):
diff = l - int(binary, 2)
for i in range(len(binary)):
if binary[i] == "0":
a1 = 2 ** (len(binary) - i - 1)
if binary[i] == "0":
if i == len(binary) - 1:
binary = binary[:i] + "1"
else:
temp = binary[i + 1 :]
temp = "".join("1" if x == "0" else "0" for x in temp)
if int(temp, 2) < diff:
binary = binary[:i] + "1" + binary[i + 1 :]
diff = diff - a1
if diff <= 0:
break
return binary
t = int(input())
for i in range(t):
x, y, l, r = [int(x) for x in input().split()]
q = x | y
if q >= l and q <= r:
if q & x == 0 or q & y == 0:
print(l)
else:
print(q)
else:
binary_r = bin(r)
binary_r = binary_r[2:]
binary_x = bin(x)
binary_x = binary_x[2:]
binary_y = bin(y)
binary_y = binary_y[2:]
binary = "0" * len(binary_r)
if len(binary_x) > len(binary_r):
binary_x = binary_x[len(binary_x) - len(binary_r) :]
else:
mi = len(binary_r) - len(binary_x)
binary_x = "0" * mi + binary_x
if len(binary_y) > len(binary_r):
binary_y = binary_y[len(binary_y) - len(binary_r) :]
else:
mi = len(binary_r) - len(binary_y)
binary_y = "0" * mi + binary_y
binary = duvidha(binary, 0)
if int(binary, 2) < l:
binary = less_than(binary, l)
if int(binary, 2) & x == 0 or int(binary, 2) & y == 0:
print(l)
else:
print(int(binary, 2)) | FUNC_DEF FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR STRING VAR VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR STRING STRING STRING VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | def process(v, l, x, y, m):
p = 50
while p >= 0:
if v >> p & 1 == 1:
k = v ^ 1 << p
if (k & x) * (k & y) == m and k >= l:
v = k
p -= 1
return v
t = int(input())
while t > 0:
inp = input()
inp = inp.split()
x = int(inp[0])
y = int(inp[1])
l = int(inp[2])
r = int(inp[3]) + 1
ans = set()
p = 50
curr = 0
while p >= 0 and r >> p & 1 == l >> p & 1:
if r >> p & 1 == 1:
curr = curr | 1 << p
p -= 1
while p >= 0:
if r >> p & 1 == 1:
ans.add(curr + ((1 << p) - 1))
curr = curr | 1 << p
p -= 1
m = 0
for v in ans:
m = max(m, (v & x) * (v & y))
res = r
for v in ans:
if (v & x) * (v & y) == m:
res = min(res, process(v, l, x, y, m))
print(res)
t -= 1 | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | def rcom(no, x, y):
orr = x | y
return no & orr
def samelen(b, l):
m = max(len(b), len(l))
b = "0" * (m - len(b)) + b
l = "0" * (m - len(l)) + l
return b, l
def round1(x, y, l, r):
x = bin(x)[2:]
y = bin(y)[2:]
l = bin(l)[2:]
r = bin(r)[2:]
rini = r
mv = (int(x, 2) & int(r, 2)) * (int(y, 2) & int(r, 2))
no = int(r, 2)
c = 0
for i in range(1, len(r)):
if r[i] == "1" and int(r[0:i] + "0" + "1" * (len(r) - i - 1), 2) >= int(l, 2):
new = r[0:i] + "0" + "1" * (len(r) - i - 1)
fv = (int(x, 2) & int(new, 2)) * (int(y, 2) & int(new, 2))
if fv >= mv and c == 0:
c = 1
mv = fv
no = int(new, 2)
elif fv > mv:
mv = fv
no = int(new, 2)
r = rini
for i in range(len(r)):
if r[i] == "1" and int(r[0:i] + "0" + "1" * (len(r) - i - 1), 2) >= int(l, 2):
new = r[0:i] + "0" + "1" * (len(r) - i - 1)
fv = (int(x, 2) & int(new, 2)) * (int(y, 2) & int(new, 2))
if fv >= mv:
mv = fv
no = int(new, 2)
else:
break
r = new
no = rcom(no, int(x, 2), int(y, 2))
if no < int(l, 2):
b = bin(no)[2:]
b, l = samelen(b, l)
for i in range(len(b)):
if b[i] == "1" and l[i] == "0":
return int(b, 2)
if b[i] == "0" and l[i] == "1":
b = b[:i] + "1" + b[i + 1 :]
return int(b, 2)
return no
t = int(input())
for _ in range(t):
st = input()
x, y, l, r = st.split()
print(round1(int(x), int(y), int(l), int(r))) | FUNC_DEF ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR STRING BIN_OP STRING BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR STRING BIN_OP STRING BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR STRING BIN_OP STRING BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR STRING BIN_OP STRING BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING RETURN FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | def checkset(n, k):
if n & 1 << k - 1:
return 1
else:
return 0
def setKthBit(n, k):
return 1 << k | n
def correct(a, b, l, r):
ans = -1
cur = 0
for i in range(l, r + 1):
if (a & i) * (b & i) > ans:
ans = (a & i) * (b & i)
cur = i
elif (a & i) * (b & i) == ans:
if i < cur:
cur = i
return cur
def myfunc(a, b, pos, precompute):
ans = 0
for i in range(pos, 0, -1):
x = checkset(a, i)
y = checkset(b, i)
if x == 1 and y == 0:
ans += precompute[i + 1]
break
if x == 0 and y == 1:
ans = setKthBit(ans, i - 1)
if x == 1 and y == 1:
ans = setKthBit(ans, i - 1)
return ans
def func(x, y, l, r):
temp = x | y
if x == 0 or y == 0:
return l
if temp >= l and temp <= r:
return temp
precompute = [0, 0]
prel = [0, 0]
val = 0
lval = 0
curr = 1
for i in range(1, 44):
if checkset(temp, i):
val += curr
precompute.append(val)
if checkset(l, i):
lval += curr
prel.append(lval)
curr *= 2
res = 0
myarr = [l]
for i in range(44, 0, -1):
a = checkset(x, i)
b = checkset(y, i)
c = checkset(r, i)
if c == 0:
continue
elif a == 0 and b == 0:
val = res + precompute[i]
if val < l:
temp = res + (precompute[i] | prel[i])
if temp < l:
res = setKthBit(res, i - 1)
continue
else:
val = res + myfunc(precompute[i], prel[i], i, precompute)
myarr.append(val)
res = 0
break
elif a == 1 and b == 1:
res = setKthBit(res, i - 1)
else:
val = res + precompute[i]
if val < l:
temp = res + (precompute[i] | prel[i])
if temp < l:
res = setKthBit(res, i - 1)
continue
val = res + myfunc(precompute[i], prel[i], i, precompute)
myarr.append(val)
res = setKthBit(res, i - 1)
myarr.append(res)
ans = -1
res1 = l
for val in myarr:
if val >= l and val <= r:
a = x & val
b = y & val
cur = a * b
if cur > ans:
ans = cur
res1 = val
if cur == ans and val < res1:
res1 = val
return res1
t = int(input())
while t > 0:
x = input().split(" ")
a = int(x[0])
b = int(x[1])
l = int(x[2])
r = int(x[3])
print(func(a, b, l, r))
t -= 1 | FUNC_DEF IF BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF RETURN BIN_OP BIN_OP NUMBER VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | def F(X, Y, Z):
return (X & Z) * (Y & Z)
def gettingabovel(lm, s):
global l
if lm & lm - 1 == 0:
x = lm | s
return x
else:
y = gettingabovel(lm & lm - 1, s)
if y >= l:
return y
return lm | s
for _ in range(int(input())):
x, y, l, r = list(map(int, input().split()))
if not x or not y:
print(l)
continue
q = x | y
if q in range(l, r + 1):
print(q)
continue
m, ans, z = F(x, y, r), r, r
while z:
temp = z - 1
if temp < l:
break
if F(x, y, temp) >= m:
m = F(x, y, temp)
ans = temp
z = z & z - 1
if F(x, y, l) >= m:
ans = l
s = ans & q
if s >= l:
print(s)
continue
x = gettingabovel(l, s)
print(x) | FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR RETURN VAR RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | def calF(zIn):
return (x & zIn) * (y & zIn)
def makeEql(str1, dis):
toAdd = ""
for _ in range(dis):
toAdd += "0"
str1 = toAdd + str1
return str1
def calZ(length, dis, ch, curList, binZ):
prev = 0
pz = l
az = r
if lenZ < length:
binZ = makeEql(binZ, dis)
dis = 0
for index in indexContainer:
newList = curList.copy()
newList[index] = ch
for j in range(index + 1, length):
newList[j] = binZ[j + dis]
z = int("".join(newList), 2)
cur = calF(z)
if cur >= prev:
if cur == prev:
if z < az:
pz = z
az = z
else:
prev = cur
pz = z
az = z
cur = calF(l)
if cur >= prev:
prev = cur
pz = l
cur = calF(r)
if cur > prev:
prev = cur
pz = r
return pz
t = int(input())
for _ in range(t):
x, y, l, r = map(int, input().split())
if l == r:
resZ = l
elif min(x, y) == 0:
resZ = l
else:
bestZ = x | y
flag = 1
if bestZ <= r and bestZ >= l:
resZ = bestZ
flag = 0
binR = bin(r).replace("0b", "")
binZ = bin(bestZ).replace("0b", "")
lenR = len(binR)
lenZ = len(binZ)
listR = list(binR)
diff = abs(lenZ - lenR)
if l == 0 and flag:
indexContainer = []
for i in range(lenR):
if listR[i] == "1":
indexContainer.append(i)
resZ = calZ(lenR, diff, "0", listR, binZ)
elif flag:
binL = bin(l).replace("0b", "")
lenL = len(binL)
difRL = lenR - lenL
binL = makeEql(binL, difRL)
lenL = lenR
for i in range(lenL):
if binL[i] != binR[i]:
break
indexContainer = []
for m in range(i, lenR):
if listR[m] == "1":
indexContainer.append(m)
resZ1 = calZ(lenR, diff, "0", listR, binZ)
if resZ1 <= r and resZ1 >= l:
resZ = resZ1
else:
listL = list(binL)
difL = abs(lenZ - lenL)
indexContainer = []
for k in range(i + 1, lenL):
if listL[k] == "0":
indexContainer.append(k)
resZ = calZ(lenL, difL, "1", listL, binZ)
print(resZ) | FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR STRING VAR VAR IF VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR STRING VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | t = int(input())
for i in range(t):
x, y, l, r = map(int, input().split(" "))
if l > r:
t = l
l = r
r = t
a = x | y
b = bin(a)[2:]
c = bin(l)[2:]
r1 = bin(r)[2:]
l1 = bin(l)[2:]
st = ""
st1 = ""
if len(r1) < len(b):
for k in range(len(b) - len(r1)):
st = st + "0"
r1 = st + r1
if len(l1) < len(b):
for k in range(len(b) - len(l1)):
st1 = st1 + "0"
l1 = st1 + l1
st1 = ""
if len(b) < len(l1):
for k in range(len(l1) - len(b)):
st1 = st1 + "0"
b = st1 + b
fix = list(r1)
fix2 = list(l1)
s = list()
if x == 0 or y == 0:
print(l)
else:
if a <= r and a >= l:
s.append(a)
for j in range(0, len(r1)):
if fix[j] == "0":
pass
else:
binary = "0b" + r1[0:j] + "0" + b[j + 1 :]
if int(binary, 2) >= l and int(binary, 2) <= r:
s.append(int(binary, 2))
s.append(r)
s.append(l)
for j in range(0, len(l1)):
if fix2[j] == "0":
pass
else:
binary = "0b" + l1[0:j] + "1" + b[j + 1 :]
if int(binary, 2) >= l and int(binary, 2) <= r:
s.append(int(binary, 2))
for j in range(len(l1) - 1, -1, -1):
if fix2[j] == "0":
pass
else:
binary = "0b" + b[0:j] + "1" + l1[j + 1 :]
if int(binary, 2) >= l and int(binary, 2) <= r:
s.append(int(binary, 2))
s = set(s)
d = {}
s1 = 0
for k in s:
s1 = (x & k) * (y & k)
if s1 in d:
d[s1].append(k)
else:
d[s1] = [k]
c = sorted(d)
h = c[-1]
print(min(d[h])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | try:
for _ in range(int(input())):
x, y, l, r = map(int, input().split())
_or = x | y | l
if 0 in [x, y, r]:
print(l)
continue
if l == 0 and r > 2 * max(x, y):
print(x | y)
continue
rBin = bin(r)[2:]
lBin = bin(l)[2:]
rLen = len(rBin)
xLen = len(bin(x)[2:])
yLen = len(bin(y)[2:])
for i in range(rLen, max(xLen, yLen)):
sb = 1 << i
ub = ~sb
x &= ub
y &= ub
if x == 0 or y == 0:
print(l)
continue
_or = x | y | l
binOr = bin(_or)[2:]
bLen = len(binOr)
dif = bLen - rLen
if dif > 0:
binOr = binOr[dif:]
elif dif < 0:
binOr = "0" * -dif + binOr
_or = int(binOr, 2)
if _or == 0:
print(l)
continue
maxVal = -1
ans = -1
bLen = len(binOr)
for i in range(bLen):
z = int(binOr, 2)
if z >= l and z <= r:
if (x & z) * (y & z) > maxVal:
maxVal = (x & z) * (y & z)
ans = z
temp = binOr
binOr = binOr[:i] + "0" + binOr[i + 1 :]
z = int(binOr, 2)
if z >= l and z <= r:
if (x & z) * (y & z) > maxVal:
maxVal = (x & z) * (y & z)
ans = z
if rBin[i] == "0":
binOr = binOr[:i] + "0" + binOr[i + 1 :]
else:
binOr = temp
if l == 0:
if maxVal == 0:
ans = l
print(ans)
continue
binAns = bin(ans)[2:]
ansLen = len(binAns)
if rLen > ansLen:
binAns = "0" * (rLen - ansLen) + binAns
else:
binAns = binAns[ansLen - rLen :]
_or1 = x | y
binOr1 = bin(_or1)[2:]
orLen = len(binOr1)
if rLen > orLen:
binOr1 = "0" * (rLen - orLen) + binOr1
else:
binOr1 = binOr1[orLen - rLen :]
for i in range(rLen):
if binOr1[i] == "0":
tmp = binAns[:i] + "0" + binAns[i + 1 :]
if int(tmp, 2) >= l:
ans = int(tmp, 2)
binAns = tmp
if maxVal == 0:
ans = l
print(ans)
except Exception as e:
print(e)
pass | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF NUMBER LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | def chk(oppo):
return (x & oppo) * (y & oppo)
t = int(input())
for _ in range(t):
x, y, l, r = map(int, input().split())
possible_ans = x | y
lbin = bin(l)[2:]
rbin = bin(r)[2:]
lbin = "0" * (len(rbin) - len(lbin)) + lbin
if possible_ans > r:
possible_ans = possible_ans & 2 ** len(rbin) - 1
pbin = bin(possible_ans)[2:]
pbin = "0" * (len(rbin) - len(pbin)) + pbin
i = 0
cur = 2 ** (len(rbin) - 1)
ans = 0
while i < len(rbin) and lbin[i] == rbin[i]:
ans = ans + cur if lbin[i] == "1" else ans
i += 1
cur = cur >> 1
i += 1
temp = cur, i
ansa = ans
cur = cur >> 1
while i < len(rbin):
if lbin[i] == "1" and pbin[i] == "1":
ansa += cur
elif lbin[i] == "1" and pbin[i] == "0":
ansa += cur
elif lbin[i] == "0" and pbin[i] == "1":
while i < len(rbin):
if pbin[i] == "1":
ansa += cur
i += 1
cur = cur >> 1
i += 1
cur = cur >> 1
cur, i = temp
ansb = ans + cur
cur = cur >> 1
while i < len(rbin):
if rbin[i] == "1" and pbin[i] == "0":
i += 1
cur = cur >> 1
while i < len(rbin):
if pbin[i] == "1":
ansb += cur
i += 1
cur = cur >> 1
elif rbin[i] == "1" and pbin[i] == "1":
ansb += cur
i += 1
cur = cur >> 1
anss = [ansb]
cur, i = temp
ansc = ans + cur
cur = cur >> 1
while i < len(rbin):
if (
rbin[i] == "1"
and pbin[i] == "1"
and chk(ansc + (cur - 1 & possible_ans)) >= chk(ansb)
):
anss.append(ansc + (cur - 1 & possible_ans))
if rbin[i] == "1" and pbin[i] == "0":
i += 1
cur = cur >> 1
while i < len(rbin):
if pbin[i] == "1":
ansc += cur
i += 1
cur = cur >> 1
elif rbin[i] == "1" and pbin[i] == "1":
ansc += cur
i += 1
cur = cur >> 1
anss.append(ansa)
anss.append(possible_ans & 2 ** len(rbin) - 1)
anss = [l] + anss
anss.append(r)
anss.append(ans)
yo = anss[0]
yoop = chk(anss[0])
for ea in anss:
zc = chk(ea)
if l <= ea <= r and (zc == yoop and ea < yo or zc > yoop):
yo, yoop = ea, zc
print(yo) | FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR IF VAR VAR STRING VAR VAR STRING WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.
Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.
Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.
Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains four space-separated integers $X$, $Y$, $L$ and $R$.
------ Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$
------ Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$
Subtask #2 (25 points): $L = 0$
Subtask #3 (60 points): original constraints
----- Sample Input 1 ------
2
7 12 4 17
7 12 0 8
----- Sample Output 1 ------
15
7
----- explanation 1 ------
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.
Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$. | for _ in range(int(input())):
x, y, l, r = map(int, input().split())
x_or_y = x | y
if l == 0 and r >= 2 * max(x, y):
if x == 0 or y == 0:
print(0)
continue
print(x_or_y)
continue
maxp = (x & l) * (y & l)
m_val = l
binr = bin(r)
binr = binr[2:]
bl = len(binr)
for i in range(bl):
if binr[i] == "1":
p = binr[:i] + "0" + "1" * (bl - i - 1)
p = int(p, 2)
if maxp < (x & p) * (y & p) and p >= l:
maxp = (x & p) * (y & p)
m_val = p
p = r
if maxp < (x & p) * (y & p):
maxp = (x & p) * (y & p)
m_val = p
bx = "{0:064b}".format(x_or_y)
bm = "{0:064b}".format(m_val)
temp = 0
for i in range(0, 64):
if bx[i] == "0" and bm[i] == "1":
temp = pow(2, 63 - i)
if m_val - temp >= l:
m_val = m_val - temp
print(m_val) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING BIN_OP STRING BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | import sys as _sys
def main():
n, p = _read_ints()
try:
result = find_min_terms_n(n, p)
except ValueError:
result = -1
print(result)
def _read_line():
result = _sys.stdin.readline()
assert result[-1] == "\n"
return result[:-1]
def _read_ints():
return map(int, _read_line().split())
def find_min_terms_n(n, p):
for m in range(1, 32 + 1):
necessary_n = n - m * p
if necessary_n < 0:
continue
x = necessary_n
active_bits_n = 0
while x:
active_bits_n += x & 1
x >>= 1
if m < active_bits_n:
continue
if m > necessary_n:
continue
return m
raise ValueError
main() | IMPORT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER STRING RETURN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
l = 1
k = 1
while n - l * p > 0:
s = bin(n - l * p)[2:]
l1 = 0
l2 = 0
last = 0
for i in s:
if i == "1":
l1 += 1
l2 += 1
last = 1
elif last > 0:
l2 += 2 ** (last - 1)
last += 1
if l >= l1 and l2 >= l:
print(l)
k = 0
break
l += 1
if k:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def Dec_to_Bin(N):
b = ""
while N > 0:
b = str(N % 2) + b
N = N // 2
if b == "":
return "0"
return b
if True:
n, p = list(map(int, input().split()))
j = 1
while True:
if n - p * j <= 0:
print(-1)
break
S = Dec_to_Bin(n - p * j)
if S.count("1") <= j:
ans = 0
k = 0
while k < len(S):
ans += int(S[k]) * 2 ** (len(S) - k - 1)
k += 1
if ans >= j:
print(j)
else:
print(-1)
break
j += 1 | FUNC_DEF ASSIGN VAR STRING WHILE VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING RETURN STRING RETURN VAR IF NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR IF FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
count = 100
if n - p == 0:
print(-1)
exit(0)
k = 0
while k <= 32:
if n - p * k >= k:
temp = bin(n - p * k)
c = str(temp)[2:].count("1")
if c <= k and c != 0:
print(k)
break
k += 1
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
for i in range(100000):
x = n - p * i
bina = bin(x)[2:].count("1")
if bina <= i and x >= i:
print(i)
exit()
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
ans = -1
for i in range(10001):
if n - i * p >= i:
c = bin(n - i * p)[2:].count("1")
if c <= i:
ans = i
break
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER STRING IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def ones(n):
result = 0
while n > 0:
n = n & n - 1
result = result + 1
return result
def can(n, p, cnt):
nn = n - p * cnt
if nn < cnt:
return False
if ones(nn) > cnt:
return False
return True
def solve(n, p):
i = 1
while i * p <= n:
if can(n, p, i):
return i
i = i + 1
return -1
def main():
n, p = [int(s) for s in input().split()]
answer = solve(n, p)
print(answer)
main() | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
k = 1
while n - p * k > 0:
count = 0
temp = n - p * k
while temp > 0:
if temp & 1:
count += 1
temp = temp >> 1
if count <= k:
break
k += 1
if n - p * k >= k:
print(k)
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def kol(a):
if a <= 0:
return -1
elif a == 1:
return 1
elif a % 2 == 1:
return kol(a // 2) + 1
else:
return kol(a // 2)
n, p = map(int, input().split())
prov = False
for i in range(1, 1000):
z = kol(n - i * p)
if z != -1 and z <= i and n - i * p >= i:
prov = True
break
if prov:
print(i)
else:
print(-1) | FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | ls = list(map(int, input().split()))
n = ls[0]
p = ls[1]
k = 1
success = False
if p > 0:
while n - k * p > 0:
x = str(bin(n - k * p))[2:]
count1 = 0
for j in x:
if j == "1":
count1 += 1
if count1 <= k and k <= n - k * p:
print(k)
success = True
break
k += 1
if success == False:
print(-1)
elif p == 0:
x = str(bin(n))[2:]
count1 = 0
for j in x:
if j == "1":
count1 += 1
if count1 > 0:
print(count1)
else:
print(-1)
elif p < 0:
while success == False:
while n - k * p < 0:
k += 1
x = str(bin(n - k * p))[2:]
count1 = 0
for j in x:
if j == "1":
count1 += 1
if count1 <= k and k <= n - k * p:
print(k)
success = True
break
k += 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER WHILE VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = list(map(int, input().split()))
for q in range(5757):
s = bin(n)
if n >= q >= s.count("1"):
print(q)
break
n -= p
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | [n, m] = list(map(int, input().split()))
def cnt(x):
y = bin(x)
y = y[2:]
return y.count("1")
ans = 1
for i in range(1, 32):
if n - i * m >= i and cnt(n - i * m) <= i:
print(i)
ans = 0
break
if ans:
print(-1) | ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def ones(n):
cnt = 0
while n > 0:
cnt += n % 2
n >>= 1
return cnt
n, p = map(int, input().split())
least = -1
for i in range(1, 40):
pbinar = n - i * p
if pbinar < 0:
break
pone = ones(pbinar)
if pone <= i <= pbinar:
least = i
break
print(least if least != -1 else -1) | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = list(map(int, input().split()))
k = 1
z = []
while len(bin(n - k * p)) - 2 >= k and n - k * p > 0:
if bin(n - k * p).count("1") <= k:
z.append(k)
k += 1
if len(z) > 0:
print(min(z))
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR STRING VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = list(map(int, input().split()))
ans = 1
n2 = n
while n2 > 0:
n2 = n - ans * p
count1 = bin(n2).count("1", 2)
if count1 <= ans and ans <= n2:
print(ans)
break
ans += 1
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
if n <= p:
print(-1)
exit()
for ans in range(1, 10**6):
x = n - p * ans
if x >= ans and bin(x).count("1") <= ans:
print(ans)
break
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def check(k, n, p):
t = n - p * k
s = bin(t)[2:]
count = 0
for i in range(len(s)):
if s[i] == "1":
count += 1
return count <= k and t >= k
def main():
n, p = map(int, input().split())
i = 1
while i < 10000:
if check(i, n, p):
print(i)
return
i += 1
print(-1)
main() | FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def set_bits(n):
cout = 0
while n > 0:
if n & 1 == 1:
cout += 1
n >>= 1
return cout
flag = 0
n, p = map(int, input().split())
for i in range(65):
num = n - i * p
ans = set_bits(num)
if i >= ans and i <= num:
flag = 1
break
if flag == 0:
print(-1)
else:
print(i) | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def pro(g, v):
if g == 0:
if v == 0:
return True
else:
return False
y = 2**100
ans = 0
x = g
while x != 0:
if x >= y:
x -= y
ans += 1
y //= 2
if ans <= v and v <= g:
return True
else:
return False
n, p = map(int, input().split())
mini = 100000
for i in range(0, 40000):
if n - p * i < 0:
break
x = pro(n - p * i, i)
if x:
mini = min(mini, i)
if mini == 100000:
print(-1)
else:
print(mini) | FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def cnt(n, a):
count = 0
p = 1
while n:
count += (n & 1) * p
if a:
p *= 2
n >>= 1
return count
def main():
n, p = map(int, input().split())
s = 1 if p < 0 else -1
ans = 10000000000
cop = n
if p == 0:
print(cnt(n, False))
return
p = abs(p)
i = -1
while i < 64:
i += 1
if cnt(n, True) >= abs(cop - n) // p and cnt(n, False) <= abs(cop - n) // p:
ans = min(ans, abs(cop - n) // p)
n += s * p
if n < 0:
break
print(-1 if ans == 10000000000 else ans)
main() | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | N, P = map(int, input().split())
def chk(k):
x = N - k * P
if x > 0 and sum(map(int, bin(x)[2:])) <= k <= x:
return 1
return 0
for i in range(1, 100):
if chk(i):
print(i)
break
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def min_summies(n, p):
for k in range(1, 50):
num = bin(n - k * p)
if num.count("1") <= k <= n - k * p:
return k
return -1
n, p = map(int, input().split())
print(min_summies(n, p)) | FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR IF FUNC_CALL VAR STRING VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
ans = 1
while n:
n -= p
if n < ans:
print(-1)
break
if bin(n).count("1") <= ans:
print(ans)
break
ans += 1
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL FUNC_CALL VAR VAR STRING VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | x, p = map(int, input().split())
ans = float("inf")
for i in range(0, 100001):
y = x - i * p
if y <= 0:
continue
aaa = 0
for j, el in enumerate(reversed(bin(y))):
if el == "1":
aaa += 2**j
if aaa >= i:
break
bbb = bin(y).count("1")
if bbb <= i <= aaa:
ans = min(ans, i)
if ans == float("inf"):
ans = -1
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR STRING VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | a, b = map(lambda x: int(x), input().split())
k = 0
s = 1
f = True
while k < s:
a = a - b
if a <= 0:
break
c = a
k += 1
s = 1
while c != 1:
s += c % 2
c = c // 2
q = k - s
q1 = k
for i in range(q):
a = a - b
if a <= 0:
break
c = a
k += 1
s = 1
while c != 1:
s += c % 2
c = c // 2
if s == k:
print(s)
f = False
break
if f and a > 0:
print(q1)
if a <= 0:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def bin(x):
return "{0:b}".format(x)
n, p = [int(i) for i in input().split()]
nA = True
k = 1
while k < 33:
if k == 32:
print(-1)
else:
t = bin(n - k * p)
a = t.count("1")
if a <= k and a > 0 and n >= k * (p + 1):
print(k)
break
k += 1 | FUNC_DEF RETURN FUNC_CALL STRING VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def bitcount(n):
count = 0
while n > 0:
count = count + 1
n = n & n - 1
return count
n, p = input("").split()
n = int(n)
p = int(p)
for i in range(1, 31):
m = n
m = m - i * p
count = bitcount(m)
if m <= 0:
print(-1)
break
elif count == i:
print(i)
break
elif count < i:
if m > i:
print(i)
break
else:
print(-1)
break | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | import time
n, p = map(int, input().split())
nt = time.time()
for i in range(1, 10000, 1):
if n - p * i < i:
break
now = bin(n - p * i)[2:].count("1")
if now <= i and now != 0:
print(i)
exit(0)
print(-1) | IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER STRING IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = [int(k) for k in input().split(" ")]
def cb(n):
res = 0
while n:
if n % 2:
res += 1
n >>= 1
return res
k = 0
res = -1
while n - k * p > 0:
nbb = cb(n - k * p)
if nbb <= k and n - k * p >= k:
res = k
break
k += 1
print(res) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
for i in range(1, 100):
x = n - i * p
if x <= 0:
print(-1)
exit()
if bin(x).count("1") <= i <= x:
print(i)
exit()
print(-1)
exit() | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL FUNC_CALL VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
def term_counter(n, counter):
if n == 1:
return counter + 1
if n % 2 == 1:
n -= 1
counter += 1
return term_counter(n, counter)
else:
while n != 1 and n % 2 != 1:
n = int(n / 2)
if n == 1:
return counter + 1
else:
n -= 1
counter += 1
return term_counter(n, counter)
i = 1
done = False
while n - p * i > 0:
if term_counter(n - p * i, 0) <= i:
if n - p * i >= i:
print(i)
done = True
break
else:
i += 1
continue
else:
i += 1
continue
if not done:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR WHILE VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
out = 1
while n:
n -= p
if n < out:
print(-1)
break
s = bin(n)
if s.count("1") <= out:
print(out)
break
out += 1 | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
m = 10000000000
q = 0
for i in range(1, 100):
a = n - i * p
if a > 0:
u = a
o = 0
while u > 0:
if u % 2 == 1:
o += 1
u //= 2
else:
u //= 2
mi = o
if i >= mi and a >= i:
print(i)
q = 1
break
else:
break
if q == 0:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def solve():
n, p = map(int, input().split())
for i in range(1, 32):
if n - i * p >= i and bin(n - i * p).count("1") <= i:
return i
return -1
print(solve()) | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR STRING VAR RETURN VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
ans = 1
fl = False
if n == p:
print(-1)
else:
while n - p * ans >= 0:
b = bin(n - p * ans)[2:].count("1")
if b <= ans <= n - p * ans:
fl = True
break
ans += 1
if fl:
print(ans)
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER STRING IF VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
f = 0
for i in range(1, 31):
x = n - i * p
b = bin(x)
if x < 0:
b = b[3:]
else:
b = b[2:]
if x >= i:
ones = 0
for j in range(len(b)):
if b[j] == "1":
ones += 1
if ones <= i:
f = 1
print(i)
break
if f == 0:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def bn(x):
b = []
i = 1
j = 0
while j < 32:
b.append(int(x & i != 0))
i = i << 1
j += 1
b.reverse()
return sum(b)
n, p = map(int, input().split())
for i in range(1, 32):
bi = bn(n - i * p)
if bi <= i <= n - i * p:
print(i)
exit()
print(-1) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | inputs = [int(x) for x in input().split()]
n = inputs[0]
y = inputs[1]
pos = False
for i in range(1000000100):
if n - i * y < i:
break
if bin(n - i * y).count("1") <= i:
print(i)
pos = True
break
if pos == False:
print(-1) | ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR IF FUNC_CALL FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
i = 1
while 1:
m = n - p * i
if m <= 0:
break
if i > m:
i += 1
continue
pc = bin(m).count("1")
if pc <= i:
print(i)
exit()
i += 1
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def get_n(bin_n):
ans = 0
while bin_n > 0:
ans += bin_n & 1
bin_n >>= 1
return ans
n, p = map(int, input().split())
ans = 1
while ans < 10**5 * 2 and not n - p * ans >= ans >= get_n(n - p * ans):
ans += 1
if ans == 10**5 * 2:
print(-1)
else:
print(ans) | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = list(map(int, input().split()))
ans = -1
l = 1
while n - l * p > 0:
if p == 0:
s = bin(n)[2:]
for i in range(len(s)):
if s[i] == "1":
ans += 1
ans += 1
break
k = n - l * p
v1, v2 = 0, 0
s = bin(k)[2:]
for i in range(len(s)):
if s[i] == "1":
v1 += 1
v2 += 1
if i > 0 and s[i - 1] == "1" and s[i] == "0":
v2 += 1
s = s[:i] + "1" + s[i + 1 :]
if v1 <= l <= v2:
ans = l
break
l += 1
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def count1(n):
return bin(n).count("1")
n, p = map(int, input().split())
if p == 0:
print(count1(n))
else:
for k in range(1, 10**8):
n1 = n - k * p
if n1 <= 0:
print(-1)
break
k1 = count1(n1)
if k1 == k:
print(k)
break
elif 0 < k - k1 <= n1 - k1:
print(k)
break | FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF NUMBER BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def check(n, p, m):
a = n - p * m
if a <= 0:
return False
b = bin(a)[2:][::-1]
l = len(b)
c = 0
res = 0
q = []
for i in range(l):
if b[i] == "1":
q += [2**i]
c += 1
res += max(0, 2 ** (i - 1))
d = c - m
if d > 0:
return False
if d == 0:
return True
if res + d > 0:
return True
def main():
n, p = [int(i) for i in input().split()]
ans = 10**4
for i in range(1, 10**4 + 1):
if check(n, p, i):
ans = i
break
if ans == 10**4:
print(-1)
else:
print(ans)
main() | FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR LIST BIN_OP NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = [int(i) for i in input().split()]
i = 1
f = True
while n - i * p >= 1:
if bin(n - i * p)[2:].count("1") <= i and i <= n - i * p:
print(i)
f = False
break
i += 1
if f:
print("-1") | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER STRING VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, k = map(int, input().split())
i = 1
n -= k
while n > 0 and not bin(n).count("1") <= i <= n:
n -= k
i += 1
if n > 0:
print(i)
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR WHILE VAR NUMBER FUNC_CALL FUNC_CALL VAR VAR STRING VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
ans = 10**18
for k in range(10**5 + 1):
x = n - k * p
if x < 0:
break
s = str(bin(x)[2:])
l = 0
r = 0
for i in range(len(s))[::-1]:
if s[i] == "1":
l += 1
r += 2 ** (len(s) - 1 - i)
if l <= k <= r:
ans = min(ans, k)
break
if ans == 10**18:
print(-1)
else:
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def get_n(n, k):
if bin(n).count("1") > k or k > n:
return False
return True
n, p = list(map(int, input().split()))
if p - n >= 0:
print(-1)
elif p - n == -1:
print(1)
else:
for i in range(1, 10101):
if get_n(n - p * i, i):
print(i)
return
print(-1) | FUNC_DEF IF FUNC_CALL FUNC_CALL VAR VAR STRING VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
aws = 1
while True:
min_number = aws * (2**0 + p)
if min_number > n:
aws = -1
break
elif min_number == n:
break
elif bin(n - aws * p).count("1") <= aws:
break
aws += 1
print(aws) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF FUNC_CALL FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | a, b = map(int, input().split())
def bn(x):
m = 0
while x > 0:
m += x % 2
x //= 2
return m
mn = 100000000
idx = 0
if b == 0:
print(bn(a))
exit(0)
else:
for n in range(1, 100):
j = n * b
if a <= j:
break
elif bn(a - j) <= n and a - j >= n:
print(n)
exit(0)
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
for k in range(41):
m = n - k * p
if m <= 0:
print(-1)
break
s = bin(m)
sumk = 0
for i in range(len(s)):
if s[-i - 1] == "1":
sumk += 2**i
if s.count("1") <= k <= sumk:
print(k)
break
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = input("").split()
n = int(n)
p = int(p)
for i in range(1, 31):
m = n
m = m - i * p
b = bin(m)
str1 = str(b)
count = str1.count("1", 0, len(str1))
if m <= 0:
print(-1)
break
elif count == i:
print(i)
break
elif count < i:
if m > i:
print(i)
break
else:
print(-1)
break | ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | BitsSetTable256 = [0] * 256
def initialize():
BitsSetTable256[0] = 0
for i in range(256):
BitsSetTable256[i] = (i & 1) + BitsSetTable256[i // 2]
initialize()
def countSetBits(n):
return (
BitsSetTable256[n & 255]
+ BitsSetTable256[n >> 8 & 255]
+ BitsSetTable256[n >> 16 & 255]
+ BitsSetTable256[n >> 24]
)
n, p = map(int, input().split())
if n < 1 + p:
print(-1)
else:
allowed = 0
while 1:
n += -p
allowed += 1
if n < 2**allowed - 1:
print(-1)
break
if countSetBits(n) <= allowed:
print(allowed)
break | ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def f(n, j):
k = 0
while n > 0 and k <= j:
m = 1
while m <= n:
m *= 2
n -= m // 2
k += 1
if k <= j:
return k
return -1
mini = 10**9
ans = -1
n, p = map(int, input().split())
for i in range(1, 10**9):
if n - p * i > 0:
if n - p * i < i:
print(-1)
break
else:
b = f(n - p * i, i)
if b != -1:
print(i)
break
else:
print(-1)
break | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | import sys
def get_one(n):
cnt = 0
while n > 0:
if n % 2 == 1:
cnt += 1
n //= 2
return cnt
n, p = map(int, input().split())
for i in range(10000):
if n > i * p and get_one(n - i * p) <= i <= n - i * p:
print(i)
sys.exit()
print(-1) | IMPORT FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | import sys
input = lambda: sys.stdin.readline().rstrip()
n, p = map(int, input().split())
ans, i = -1, 1
while True:
n -= p
if n <= 0:
break
if bin(n).count("1") <= i and i <= n:
ans = i
break
i += 1
print(ans) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER WHILE NUMBER VAR VAR IF VAR NUMBER IF FUNC_CALL FUNC_CALL VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def get(n, k):
return n >> k & 1
def cal(n):
sum = 0
for i in range(30):
sum += get(n, i)
return sum
n, k = map(int, input().split())
rs = -1
for i in range(30):
X = n - i * k
if cal(X) <= i and X >= i:
rs = i
break
print(rs) | FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | out = -1
n, p = [int(i) for i in input().split()]
for i in range(1, 10000, 1):
if n - p * i < i:
break
answer = str(bin(n - p * i)[2:]).count("1")
if answer <= i and answer != 0:
out = i
break
print(out) | ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER STRING IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
n -= p
f = False
for i in range(100):
s = bin(n)
if len(s) - 2 >= i + 1 >= s.count("1") > 0 and n > 0:
k = i + 1
f = True
break
else:
n -= p
if f:
print(k)
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR STRING NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
def count(s):
c = 0
b = list(bin(s))
b.pop(0)
b.pop(0)
for i in b:
if i == "1":
c += 1
return c
i = 1
if p > 0:
l = n / (p + 1)
while i <= l:
if count(n - i * p) <= i:
print(i)
break
else:
i += 1
else:
print("-1")
else:
i = 1
while True:
if count(n - i * p) <= i:
print(i)
break
else:
i += 1 | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER RETURN VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
def countSetBits(num):
binary = bin(num)
if num < 0:
binary = binary[1:]
setBits = [ones for ones in binary[2:] if ones == "1"]
return len(setBits)
found = False
for i in range(1, 33):
if n - i * p >= i and countSetBits(n - i * p) <= i:
found = True
print(i)
break
if not found:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR STRING RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
i = 0
while True:
if n - p * i < 0:
print(-1)
break
if bin(n - p * i).count("1") <= i and i <= n - p * i:
print(i)
break
i += 1 | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR STRING VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
ans = -1
i = 1
while True:
n -= p
if n < i:
print(ans)
break
k = str(bin(n)[2:]).count("1")
if i >= k:
print(i)
break
i += 1 | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER STRING IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
ones = "{0:b}".format(n).count("1")
t = 0
while (ones > t or n < t) and n > 0:
n -= p
t += 1
ones = "{0:b}".format(n).count("1")
if n <= 0:
print(-1)
else:
print(t) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL STRING VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL STRING VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | import sys
def count_1(n):
ans = 0
while n > 0:
if n % 2 == 1:
ans += 1
n //= 2
return ans
n, p = list(map(int, input().split()))
for i in range(1, 40):
n -= p
if n <= 0:
break
if count_1(n) <= i and n >= i:
print(i)
return
print(-1) | IMPORT FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def bit_count(A, i):
answ = 0
j = 0
d = []
while A > 0:
if A & 1:
answ += 1
A >>= 1
d.append(j)
j += 1
if answ > 0:
while answ < i and d:
if d[-1] > 1:
answ += 1
nj = d.pop()
d.append(nj)
d.append(nj)
elif d[-1] == 1:
answ += 1
d.pop()
else:
d.pop()
return answ
n, p = map(int, input().split())
i = 0
while bit_count(n - p * i, i) != i and i < 5757:
i += 1
if i < 5757:
print(i)
else:
print(-1) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER WHILE VAR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
if p == 0:
dv = str(bin(n))[2:]
colvo = dv.count("1")
print(colvo)
else:
m = 1
while n - m * p >= 0:
a = n - m * p
dv = str(bin(a))[2:]
colvo = dv.count("1")
if colvo <= m and m <= a:
print(m)
exit(0)
m += 1
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def dectobin(n, out):
s = 0
while n:
s += n % 2
n //= 2
return s <= out
n, p = list(map(int, input().split()))
out = 0
f = 0
while n - p >= out + 1:
n -= p
out += 1
if dectobin(n, out):
f = 1
break
if f == 1:
print(out)
else:
print(-1) | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
for k in range(1, 50):
num = bin(n - k * p)
if num.count("1") <= k <= n - k * p:
print(k)
exit()
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR IF FUNC_CALL VAR STRING VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def countSetBits(n):
count = 0
while n:
n &= n - 1
count += 1
return count
t = 1
for _ in range(t):
n, p = map(int, input().split())
count = 0
found = False
while n > 0 and not found and count < 32:
n -= p
count += 1
if n < 0:
break
x = countSetBits(n)
if x <= count and n >= count:
found = True
if found:
print(count)
else:
print(-1) | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
for i in range(1, 33):
k = n - i * p
if k >= i and bin(abs(k)).count("1") <= i:
print(i)
exit(0)
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def decimal_to_binary(x):
res = []
while x > 0:
res.append(x % 2)
x //= 2
return res[::-1]
n, p = list(map(int, input().split()))
for k in range(50):
x = n - k * p
if x < 0:
break
res = decimal_to_binary(x).count(1)
if res <= k and res > 0 and n - k * p >= k:
print(k)
exit(0)
print(-1) | FUNC_DEF ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def f(x):
return range(sum([int(e) for e in bin(x)[2:]]), x + 1)
def main():
n, p = [int(e) for e in input().split()]
b = 1
while n - b * p >= 0:
d = f(n - b * p)
if b in d:
print(b)
return
if b > d[-1]:
break
b += 1
print(-1)
main() | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def f(n, p, k):
n = n - k * p
ans = 0
l = 0
z = []
while n >= 1:
ans += n % 2
z.append(n % 2)
n //= 2
for i in range(len(z)):
l += z[i] * 2**i
if ans <= k <= l:
return ans
else:
return 10**4
n, p = map(int, input().split())
k = 10000
ans = 10**4
for i in range(1, 10**4):
if f(n, p, i) != 10**4:
print(i)
exit(0)
print(-1) | FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR VAR VAR RETURN VAR RETURN BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, k = map(int, input().split())
f = 0
c = 0
for i in range(35):
d = n
c = 0
d = n - i * k
t = d
while d > 0:
c += d % 2
d //= 2
if c <= i and i <= t:
f = 1
print(i)
break
if f == 0:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = [int(x) for x in input().split()]
l = 1
for mid in range(1, 32):
k = n - mid * p
u = 0
if k < mid:
continue
for i in range(50, -1, -1):
if k >= 2**i:
k -= 2**i
u += 1
if k == 0:
break
if u <= mid and u != 0:
print(mid)
break
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def solve():
n, p = map(int, input().split())
for k in range(1, 32):
n1 = n - k * p
if n1 < k:
continue
b = 0
while n1 > 0:
b += n1 % 2
n1 //= 2
if b <= k:
print(k)
return
print(-1)
solve() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | def Find(n, k):
sum = k
A = [(1) for i in range(k)]
i = k - 1
while i >= 0:
while sum + A[i] <= n:
sum += A[i]
A[i] *= 2
i -= 1
if sum != n:
return -1
else:
return 0
n, p = map(int, input().split())
k = 1
f = 0
while k <= 40:
if Find(n - p * k, k) == 0:
f = 1
break
k += 1
if f == 1:
print(k)
else:
print(-1) | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = [int(i) for i in input().split()]
def g(x: int, k: int) -> list:
q = bin(x)[2:][::-1]
r1, r2, r3, r = [], [], [], []
s = sum(map(int, list(q)))
i = len(q) - 1
while s < k:
qq = int(q[i]) * 2 + int(q[i - 1])
ost = ""
if len(q[i:]) > 1:
ost = "0" + q[i + 1 :]
s = sum(map(int, list(q[: i - 1] + ost)))
input(f"\t(до) q = {q}\n\ti = {i}, s = {s}, qq = {qq}\n")
if s + qq > k:
i -= 1
continue
q = q[: i - 1] + str(qq) + ost
i -= 1
s = sum(map(int, list(q)))
input(f"\t(после) q = {q}\n\ti = {i}, s = {s}, qq = {qq}\n")
for i in range(len(q)):
j = int(q[i])
if j >= 1:
r += [f"2^{i}"] * j
r1.append(f"{['', f'{j}*'][j > 1]}" + f"2^{i}")
r2.append(f"{['', f'{j}*'][j > 1]}" + f"{2 ** i}")
r3.append(f"{j * 2 ** i}")
return r[::-1], r1, r2, r3
if n < 0 or n == 0 and p == 0:
exit("-1")
if p == 0:
print(bin(n).count("1"))
else:
b = n
k = -1
z = p / abs(p)
if n - p >= 1:
while (b + k * p) * z > n * z or k == -1:
b -= p
b2 = bin(b)[2:]
k = b2.count("1")
k = (n - b) // p
if b >= k:
print(k)
else:
print(-1)
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR LIST LIST LIST LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP STRING VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP LIST STRING VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST STRING VAR STRING VAR NUMBER STRING VAR EXPR FUNC_CALL VAR BIN_OP LIST STRING VAR STRING VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER WHILE BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | n, p = map(int, input().split())
power = []
power.append(1)
for i in range(1, 31):
power.append(power[len(power) - 1] * 2)
ans = -1
i = 1
while i < 31:
y = n - i * p
if y <= 0 or y < i:
break
a = []
while y:
a.append(y % 2)
y = y // 2
count = 0
for x in a:
if x:
count += 1
if count <= i:
ans = i
break
i += 1
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.