description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
a = [int(C) for C in input().split()]
mat = [["0" for i in range(20)] for j in range(n)]
for i in range(20):
q = 0
for j in a:
if j & 1 << i:
mat[q][i] = "1"
q += 1
x = 0
for i in mat:
x += int("".join(i[::-1]), 2) ** 2
print(x) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
aa = [int(i) for i in input().split()]
bits = [0] * 21
for i in range(0, n):
b = bin(aa[i])[2:][::-1]
for j in range(0, len(b)):
bits[j] += int(b[j])
ans = 0
for i in range(0, n):
ss = 0
for j in range(0, 21):
if bits[j]:
ss += pow(2, j)
bits[j] -= 1
ans += pow(ss, 2)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | bits = 20
n = int(input())
a = list(map(int, input().split()))
counts = [0] * bits
for x in a:
for i in range(bits):
counts[i] += x & 1
x >>= 1
b = [0] * n
for i, count in enumerate(counts):
for j in range(count):
b[j] |= 1 << i
res = sum(x**2 for x in b)
print(res) | ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | from sys import stdout
n = int(input())
ls = list(map(int, input().split()))
ar = [0] * 20
for i in range(n):
s = bin(ls[i])[2:]
k = len(s)
j = 0
while ls[i]:
ar[19 - j] += ls[i] & 1
ls[i] >>= 1
j += 1
ans = 0
for i in range(n):
s = ""
cnt = 0
for j in range(20):
if ar[j] > 0:
cnt += 1 << 19 - j
ar[j] -= 1
ans += cnt**2
stdout.write(str(ans)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | def inp():
return int(input())
def inlt():
return list(map(int, input().split()))
def invr():
return map(int, input().split())
test_count = 1
for t in range(test_count):
n = inp()
a = inlt()
counts = {}
max_digit = 0
for x in a:
b = bin(x)[:1:-1]
for i in range(len(b)):
if b[i] == "1":
counts[i] = counts.get(i, 0) + 1
max_digit = max(max_digit, i)
ans = 0
i = max_digit
while i >= 0:
if i in counts and counts[i] > 0:
num = ""
for j in range(i, -1, -1):
if j in counts and counts[j] > 0:
num += "1"
counts[j] = counts[j] - 1
else:
num += "0"
dec_num = int(num, 2)
ans += dec_num * dec_num
if i not in counts or counts[i] == 0:
i -= 1
print(ans) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
l = [0] * 20
a = map(int, input().split())
for i in a:
x = i
y = 0
while x > 0:
if x % 2 == 1:
l[y] += 1
x -= 1
y += 1
x /= 2
sol = 0
for _ in range(n):
x = 0
for i in range(20):
if l[i] > 0:
x += pow(2, i)
l[i] -= 1
sol += x * x
print(sol) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | def prog():
n = int(input())
nums = list(map(int, input().split()))
amounts = [0] * 20
for num in nums:
num = bin(num)[:1:-1]
for i in range(len(num)):
amounts[i] += "1" == num[i]
totals = []
while True:
left = False
for amount in amounts:
if amount:
left = True
break
if not left:
break
curr = 0
for i in range(20):
if amounts[i]:
amounts[i] -= 1
curr += 1 << i
totals.append(curr)
ans = 0
for total in totals:
ans += total**2
print(ans)
prog() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING VAR VAR ASSIGN VAR LIST WHILE NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
arr = list(map(int, input().split()))
s = [0] * 20
for i in arr:
num = i
t = 0
while num > 1:
s[t] += num % 2
num = num // 2
t += 1
s[t] += num % 2
total = 0
while s != [0] * 20:
ans = ""
for i in range(20):
if s[i] > 0:
ans += "1"
s[i] -= 1
else:
ans += "0"
total += pow(int(ans[::-1], 2), 2)
print(total) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR STRING VAR VAR NUMBER VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | from sys import stdin, stdout
n = int(stdin.readline().strip())
arr = list(map(int, stdin.readline().strip().split(" ")))
count = [(0) for i in range(22)]
for i in arr:
ctr = 0
while i > 0:
if i & 1:
count[ctr] += 1
ctr += 1
i = i >> 1
farr = [(0) for i in range(n)]
for i in range(len(count)):
for j in range(count[i]):
farr[j] = farr[j] | 1 << i
ans = 0
for i in farr:
ans += i**2
stdout.write(str(ans) + "\n") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
a = list(map(int, input().split()))
bitCount = [0] * 20
for elem in a:
binElem = bin(elem)[2:]
lenBinElem = len(binElem)
for i in range(lenBinElem):
if binElem[i] == "1":
bitCount[lenBinElem - 1 - i] += 1
ans = 0
for i in range(n):
curElem = 0
for j in range(20):
if bitCount[j] > i:
curElem += 2**j
ans += curElem**2
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
n = int(input())
arr = [int(x) for x in input().split()]
LOG = 20
bits = [(0) for x in range(LOG)]
for item in arr:
for i in range(LOG):
if 1 << i & item:
bits[i] += 1
ans = []
while sum(bits):
num = 0
for i in range(LOG):
if bits[i]:
num |= 1 << i
bits[i] -= 1
ans.append(num)
print(sum([(item**2) for item in ans])) | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | import sys
input = sys.stdin.readline
def inp():
return int(input())
def inlt():
return list(map(int, input().split()))
def insr():
s = input()
return list(s[: len(s) - 1])
def invr():
return map(int, input().split())
inlt()
array = inlt()
def bina(n, a):
i = 0
while n > 0:
if i + 1 > len(a):
a = a + [0]
if n % 2 == 1:
a[i] = a[i] + 1
n = n // 2
i = i + 1
return a
a = [0]
for i in range(len(array)):
a = bina(array[i], a)
ans = 0
while sum(a) > 0:
t = 0
for i in range(len(a)):
if a[i] > 0:
a[i] = a[i] - 1
t = t + 2**i
ans = ans + t**2
print(ans) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR LIST NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | def help():
nos = [0] * 20
n = int(input())
arr = list(map(int, input().split(" ")))
for i in range(n):
for j in range(20):
if arr[i] >> j & 1:
nos[j] += 1
ans = [0] * n
for qw in range(20):
for i in range(n):
if nos[qw] == 0:
break
ans[i] |= 1 << qw
nos[qw] -= 1
toret = 0
for i in range(n):
toret += ans[i] ** 2
print(toret)
help() | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | import sys
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.readline())
def MI():
return map(int, sys.stdin.readline().split())
def LI():
return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def SI():
return sys.stdin.readline()[:-1]
def main():
n = II()
aa = LI()
cnt = [0] * 20
for a in aa:
for i in range(20):
if a & 1:
cnt[i] += 1
a >>= 1
ans = 0
for _ in range(n):
cur = 0
for i in range(20):
if cnt[i] == 0:
continue
cnt[i] -= 1
cur += 1 << i
ans += cur**2
print(ans)
main() | IMPORT ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
a = input().split()
b = [0] * 30
for i in a:
x = 0
while x <= 20:
if int(i) & 1 << x != 0:
b[x] += 1
x += 1
res = 0
while n > 0:
n -= 1
temp = 0
index = 0
while index <= 20:
if b[index] > 0:
b[index] -= 1
temp |= 1 << index
index += 1
res += temp * temp
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | from sys import stdin
n = int(stdin.readline())
a = [int(x) for x in stdin.readline().split()]
bitI = [(0) for x in range(22)]
for x in a:
b = 0
while x > 0:
if x % 2 == 1:
bitI[b] += 1
b += 1
x = x >> 1
total = 0
for x in range(max(bitI)):
num = 0
for y in range(22):
if bitI[y]:
num += 2**y
bitI[y] -= 1
total += num**2
print(total) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
g = [0] * 21
c = a = 0
for i in map(int, input().split()):
b = bin(i)[2:].zfill(21)
for j in range(21):
if b[j] == "1":
c += 1
g[j] += 1
while c:
t = ""
for i in range(20, -1, -1):
if g[i]:
t = "1" + t
c -= 1
g[i] -= 1
else:
t = "0" + t
a += int(t, 2) ** 2
print(a) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR VAR NUMBER WHILE VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP STRING VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | from sys import stdin, stdout
rr = lambda: input().strip()
rri = lambda: int(rr())
rrm = lambda: [int(x) for x in rr().split()]
def sol():
n = rri()
v = rrm()
x = [(0) for i in range(25)]
for i in v:
z = i
j = 0
while z > 0:
if z & 1 == 1:
x[j] += 1
z = z >> 1
j += 1
for i in range(n):
v[i] = 0
for j in range(25):
if x[j] > i:
z = 1 << j
v[i] += z
res = 0
for i in v:
res += i * i
print(res)
return
sol() | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | import sys
pprint = lambda s: print(" ".join(map(str, s)))
input = lambda: sys.stdin.readline().strip()
ipnut = input
n = int(input())
a = list(
map(lambda x: list(reversed(list(map(int, bin(int(x))[2:])))), input().split())
)
lox = {}
for i in a:
for j in range(len(i)):
if i[j]:
lox[j] = lox.get(j, 0) + 1
ans = [(0) for i in range(n)]
for b, k in lox.items():
d = 2**b
for i in range(k):
ans[i] += d
print(sum(map(lambda x: x**2, ans))) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | t = 1
while t != 0:
t -= 1
n = int(input())
lt = [int(x) for x in input().split()]
if n == 1:
print(lt[0] ** 2)
else:
bits = [(0) for _ in range(20)]
for i in range(n):
k = lt[i]
for j in range(20):
if k % 2 != 0:
bits[j] += 1
k //= 2
ans = ["" for _ in range(n)]
for i in range(20):
for j in range(bits[i]):
ans[j] += "1"
for j in range(bits[i], n):
ans[j] += "0"
sm = 0
for i in ans:
p = int(i[::-1], 2)
sm += p**2
print(sm) | ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
l = list(map(int, input().split()))
ans = []
for i in range(20):
ans.append([])
for i in range(n):
tmp = bin(l[i])[2:]
ln = 20 - len(tmp)
tmp = "0" * ln + tmp
for j in range(20):
ans[j].append(tmp[j])
for i in range(20):
ans[i].sort()
fin = 0
for i in range(n):
tmp = 0
for j in range(20):
tmp += int(ans[j][i]) * 2 ** (20 - j - 1)
fin += tmp * tmp
print(fin) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
lis = sorted(map(int, input().split()))
bit = [0] * 21
for i in range(21):
k = 2**i
for j in range(n):
if k & lis[j]:
bit[i] += 1
ans = 0
while sum(bit) != 0:
no = 0
for i in range(21):
if bit[i]:
no += 2**i
bit[i] -= 1
ans += no * no
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | import sys
ints = (int(x) for x in sys.stdin.read().split())
sys.setrecursionlimit(3000)
def main():
n = next(ints)
m = [next(ints) for i in range(n)]
z = [0] * 20
for x in m:
i = 0
while x:
z[i] += x & 1
x >>= 1
i += 1
ans = 0
while 1:
while z and z[-1] == 0:
z.pop()
if not z:
break
x = 0
for i in range(len(z)):
if z[i]:
x += 2**i
z[i] -= 1
ans += x * x
print(ans)
return
main() | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER WHILE VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
a = [int(i) for i in input().split()]
d = 20
b = []
x = []
for i in range(d):
x += [[(j >> i & 1) for j in a]]
b += [sorted(x[-1])]
ans = []
for j in range(n):
t = 0
for i in range(d):
t += b[i][j] << i
ans += [t]
print(sum([(i**2) for i in ans])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR LIST BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR LIST FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
l = list(map(int, input().split()))
ln = len(bin(max(l))[2:])
l2 = [0] * ln
for i in l:
x = list(map(int, bin(i)[2:]))
x = [0] * (ln - len(x)) + x
l2 = [sum(j) for j in zip(l2, x)]
s = 0
for i in range(max(l2)):
x = ""
for j in range(len(l2)):
if l2[j] > 0:
l2[j] -= 1
x += "1"
else:
x += "0"
s += int(x, 2) ** 2
print(s) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR STRING VAR STRING VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
a = list(map(int, input().split()))
x = [0] * 20
for i in range(n):
s = "{0:b}".format(a[i])
b = []
for j in range(len(s)):
b.append(s[j])
b.reverse()
for j in range(20 - len(s)):
b.append("0")
b.reverse()
for j in range(20):
if b[j] == "1":
x[j] += 1
ans = 0
for i in range(n):
s = []
for j in range(20):
if x[j] > 0:
s.append("1")
x[j] -= 1
else:
s.append("0")
ans += int("".join(s), 2) ** 2
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR BIN_OP FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
a = list(map(int, input().split()))
d = [(0) for i in range(20)]
for i in a:
t = bin(i).replace("0b", "")
for i in range(len(t) - 1, -1, -1):
if t[i] == "1":
d[19 - (len(t) - 1 - i)] += 1
ans = 0
while 1:
mul = 1
num = 0
f = 0
for i in range(19, -1, -1):
if d[i]:
f = 1
d[i] -= 1
num += mul
mul *= 2
ans += num * num
if f == 0:
break
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | def ans(A):
count = [0] * 25
for a in A:
s = "{0:b}".format(a)
s = list(s[::-1])
for i, e in enumerate(s):
if e == "1":
count[i] += 1
ret = 0
while sum(count):
next = [0] * 25
for i in range(25):
if count[i] > 0:
count[i] -= 1
next[i] += 1
next = "".join(str(x) for x in next)
next = int(next[::-1], 2)
ret += next**2
return ret
input()
A = input().split(" ")
A = [int(a) for a in A]
print(ans(A)) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
a = list(map(int, input().split()))
b = [0] * n
for u in range(20):
for i in range(sum(x >> u & 1 for x in a)):
b[i] |= 1 << u
print(sum(x * x for x in b)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | import sys
def inp():
return sys.stdin.readline().strip()
n = int(inp())
a = list(map(int, inp().split()))
freq = {}
for i in range(32):
freq[i] = 0
mask = 1 << i
for j in a:
if j & mask > 0:
freq[i] += 1
ans = [0] * n
for i in range(31, -1, -1):
if freq[i] <= 0:
continue
mask = 1 << i
for j in range(n - 1, -1, -1):
if freq[i] > 0:
ans[j] |= mask
freq[i] -= 1
sm = 0
for i in ans:
sm += i * i
print(sm) | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | from sys import stdin, stdout
def and_or_and_square_sum(n, a):
bcnt = [(0) for i in range(32)]
for v in a:
for i in range(32):
if v & 1 << i > 0:
bcnt[i] += 1
res = 0
cur = -1
while cur != 0:
cur = 0
for i in range(32):
if bcnt[i] > 0:
cur += 1 << i
bcnt[i] -= 1
res += cur * cur
return res
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
print(and_or_and_square_sum(n, a)) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | def inp():
return int(input())
def inlt():
return list(map(int, input().split()))
def insr():
s = input()
return list(s[: len(s)])
def invr():
return map(int, input().split())
n = inp()
z = []
for i in range(20):
z.append(2**i - 1)
l = inlt()
l.sort()
b = [0] * n
for bit in range(20):
cnt = 0
for j in range(n):
cnt += l[j] >> bit & 1
for i in range(cnt):
b[i] |= 1 << bit
res = 0
for x in b:
res += x**2
print(res) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
a = [bin(i)[2:] for i in list(map(int, input().split()))]
a = [list((20 - len(i)) * "0" + i) for i in a]
for i in range(20):
ones = sum([(j[i] == "1") for j in a])
l = ["0"] * (n - ones) + ["1"] * ones
for j in range(n):
a[j][i] = l[j]
s = 0
for i in a:
s += int("".join(i), 2) ** 2
print(s) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR STRING VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR STRING VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING BIN_OP VAR VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | import sys
lines = sys.stdin.readlines()
n = int(lines[0].strip())
arr = list(map(int, lines[1].strip().split(" ")))
digits = [(0) for _ in range(21)]
for a in arr:
b = bin(a)[2:]
for i in range(1, len(b) + 1):
if b[-i] == "1":
digits[i - 1] += 1
final = []
for i in range(n):
tmp = 0
for j in range(21):
if digits[j] != 0:
tmp += 2**j
digits[j] -= 1
final.append(tmp)
res = 0
for num in final:
res += num**2
print(res) | IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | def getmini(arr):
maxi = 1000000000000
for i in arr:
if i > 0:
maxi = min(maxi, i)
return maxi
n = int(input())
arr = list(map(int, input().split()))
if n == 1:
print(arr[0] * arr[0])
exit()
else:
freq = [(0) for i in range(21)]
for i in arr:
b = bin(i)[2:]
if len(b) < 21:
b = "0" * (21 - len(b)) + b
for z in range(21):
if b[z] == "1":
freq[z] += 1
ans = 0
while freq.count(0) != 21:
cnt = getmini(freq)
st = ["0" for i in range(21)]
for i in range(21):
if freq[i] == 0:
pass
else:
freq[i] -= cnt
st[i] = "1"
el = int("".join(st), 2)
ans += cnt * pow(el, 2)
print(ans) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | def solve():
nm = [(0) for i in range(21)]
step = [1]
for i in range(20):
step.append(step[-1] * 2)
n = int(input())
lst = list(map(int, input().split()))
for i in range(n):
num = lst[i]
st = 0
while num:
ar = num % 2
nm[st] += ar
num //= 2
st += 1
ans = 0
for i in range(n):
rnum = 0
for j in range(21):
if nm[j] > 0:
nm[j] -= 1
rnum += step[j]
ans += rnum * rnum
print(ans)
for i in range(1):
solve() | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
a = list(map(int, input().split()))
deg_count = []
for i in range(n):
deg = 0
while a[i] > 0:
if deg > len(deg_count) - 1:
deg_count.append(0)
if a[i] & 1:
deg_count[deg] += 1
a[i] >>= 1
deg += 1
ans = 0
for i in range(n):
num = 0
for deg in range(len(deg_count)):
if deg_count[deg] > 0:
num += 1 << deg
deg_count[deg] -= 1
ans += num * num
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
if n == 1:
print(a[0] ** 2)
exit()
a.sort(reverse=True)
x = len(bin(a[0])[2:])
b = []
for i in range(n):
y = bin(a[i])[2:]
y = "0" * (x - len(y)) + y
b.append(list(y))
for i in range(x):
count = 0
for j in range(n):
if b[j][i] == "1":
count += 1
for j in range(count):
b[j][i] = "1"
for j in range(count, n):
b[j][i] = "0"
ans = 0
for i in range(n):
y = ""
for j in range(x):
y += b[i][j]
ans += int(y, 2) ** 2
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
a = list(map(int, input().split()))
cnt = [(0) for i in range(32)]
for i in range(n):
for j in range(32):
if a[i] % 2:
cnt[j] += 1
a[i] //= 2
pows = [(2**i) for i in range(35)]
for j in range(32):
for i in range(cnt[j]):
a[i] += pows[j]
sum1 = 0
for i in range(n):
sum1 += a[i] * a[i]
print(sum1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | import sys
readline = sys.stdin.readline
N = int(readline())
A = list(map(int, readline().split()))
B = 20
table = [0] * B
for a in A:
for i in range(B):
if 1 << i & a:
table[i] += 1
ans = 0
for _ in range(N):
res = 0
for i in range(B):
if table[i]:
table[i] -= 1
res |= 1 << i
ans += res**2
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
u = list(map(int, input().split()))
d = [0] * 20
for i in range(n):
ind = 0
while u[i] > 0:
d[ind] += u[i] % 2
ind += 1
u[i] //= 2
for i in range(20):
d[i] = d[i], i
d.sort()
ans = 0
cur = 2**20 - 1
dl = 0
for i in range(20):
ans += (d[i][0] - dl) * cur**2
dl += d[i][0] - dl
cur -= 2 ** d[i][1]
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
a = list(map(int, input().split()))
ct = [0] * 30
for x in a:
for sf in range(30):
ct[sf] += x >> sf & 1
ans = [0] * n
for i in range(30):
for j in range(ct[i]):
ans[j] |= 1 << i
print(sum([(x**2) for x in ans])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | def solution(A, n):
C = [int(0) for _ in range(20)]
for i in range(n):
for j in range(20):
C[19 - j] += A[i] >> j & 1
total = 0
for i in range(n):
curr = 0
for j in range(20):
if C[j] > 0:
C[j] -= 1
curr += 1 << 19 - j
total += curr**2
return total
n = int(input())
A = list(map(int, input().split(" ")))
print(solution(A, n)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
A = list(map(int, input().split(" ")))
count = [(0) for i in range(20)]
exp = [(1) for i in range(21)]
exp[1] = 2
for i in range(2, 21):
exp[i] = 2 * exp[i - 1]
def COUNT(x):
c = 0
while x != 0:
if x & 1 == 1:
count[c] += 1
x >>= 1
c += 1
for i in A:
COUNT(i)
ans = 0
for i in range(n):
temp = 0
for i in range(19, -1, -1):
if count[i] > 0:
temp += exp[i]
count[i] -= 1
ans += temp * temp
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
l = list(map(int, input().split()))
b = [0] * 21
ans = 0
for i in l:
x = bin(i)[2:][::-1]
for j in range(len(x)):
b[j] += int(x[j])
while 1:
f = 0
s = ""
for i in range(20, -1, -1):
if b[i] > 0:
s += "1"
b[i] -= 1
else:
s += "0"
ans += int(s, 2) * int(s, 2)
if int(s, 2) == 0:
break
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR STRING VAR VAR NUMBER VAR STRING VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
a = sorted(list(map(int, input().split())))
b = [(0) for i in range(n)]
for i in range(20):
count = 0
for j in range(n):
if a[j] & 1 << i:
count += 1
for j in range(n - 1, -1, -1):
if count <= 0:
break
b[j] = b[j] | 1 << i
count -= 1
ans = 0
for i in b:
ans += i**2
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | import sys
n = int(sys.stdin.readline().strip())
a = list(map(int, sys.stdin.readline().strip().split()))
C = [0] * 20
for i in range(0, n):
for j in range(0, 20):
if a[i] % 2 == 1:
C[j] = C[j] + 1
a[i] = a[i] // 2
for j in range(0, 20):
for i in range(0, C[j]):
a[i] = a[i] + 2**j
ans = 0
for i in range(0, n):
ans = ans + a[i] ** 2
print(ans) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
A = list(map(int, input().split()))
bitcount = [0] * 20
for a in A:
b = bin(a + 2**20)
for i in range(20):
if b[22 - i] == "1":
bitcount[i] += 1
summa = 0
while max(bitcount) > 0:
res = list(filter(lambda i: bitcount[i] > 0, range(20)))
el = 0
for i in range(20):
if i in res:
el += 2**i
bitcount[i] -= 1
summa += el**2
print(summa) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP NUMBER VAR STRING VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | def main():
n = int(input())
nums = [int(x) for x in input().split()]
bits = {}
for i in range(20):
bits[i] = 0
for num in nums:
x = 0
while num > 0:
if num % 2 == 1:
bits[x] += 1
num -= 1
num /= 2
x += 1
mysum = 0
temp = []
for x in bits:
if bits[x] == 0:
temp.append(x)
for x in temp:
bits.pop(x)
while len(bits) > 0:
temp = []
remaining_bits = [x for x in bits]
y = min(bits[x] for x in bits)
mysum += sum(2**x for x in remaining_bits) ** 2 * y
for x in bits:
bits[x] -= y
if bits[x] == 0:
temp.append(x)
for x in temp:
bits.pop(x)
return mysum
output = main()
print("{0}".format(output)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR FOR VAR VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of $n$ non-negative integers $a_1, \ldots, a_n$. You are allowed to perform the following operation: choose two distinct indices $1 \leq i, j \leq n$. If before the operation $a_i = x$, $a_j = y$, then after the operation $a_i = x~\mathsf{AND}~y$, $a_j = x~\mathsf{OR}~y$, where $\mathsf{AND}$ and $\mathsf{OR}$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute $\sum_{i=1}^n a_i^2$ — the sum of squares of all $a_i$. What is the largest sum of squares you can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i < 2^{20}$).
-----Output-----
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
-----Examples-----
Input
1
123
Output
15129
Input
3
1 3 5
Output
51
Input
2
349525 699050
Output
1099509530625
-----Note-----
In the first sample no operation can be made, thus the answer is $123^2$.
In the second sample we can obtain the collection $1, 1, 7$, and $1^2 + 1^2 + 7^2 = 51$.
If $x$ and $y$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $x~\mathsf{AND}~y$ is set to $1$ if and only if both corresponding bits of $x$ and $y$ are set to $1$. Similarly, each bit of $x~\mathsf{OR}~y$ is set to $1$ if and only if at least one of the corresponding bits of $x$ and $y$ are set to $1$. For example, $x = 3$ and $y = 5$ are represented as $011_2$ and $101_2$ (highest bit first). Then, $x~\mathsf{AND}~y = 001_2 = 1$, and $x~\mathsf{OR}~y = 111_2 = 7$. | n = int(input())
arr = [int(x) for x in input().split()]
setBitCounts = [(0) for _ in range(20)]
for x in arr:
for i in range(20):
if x & 1 << i > 0:
setBitCounts[i] += 1
t = 0
while sum(setBitCounts) > 0:
minNonZero = 2 * 10**5
for c in setBitCounts:
if 0 < c < minNonZero:
minNonZero = c
b = 0
for i in range(20):
if setBitCounts[i] > 0:
b = b | 1 << i
setBitCounts[i] -= minNonZero
t += b * b * minNonZero
print(t) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FOR VAR VAR IF NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | n, m = map(int, input().split())
d = {}
temp = []
for i in range(1, m + 1):
lb = i & -i
if lb not in d:
d[lb] = []
d[lb].append(i)
temp.append(lb)
temp = sorted(temp)[::-1]
ans = []
for j in temp:
if j <= n and len(d[j]):
n -= j
ans.append(d[j][-1])
d[j].pop()
if n == 0:
print(len(ans))
print(*ans)
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | s, l = map(int, input().split())
val = []
for i in range(1, l + 1):
for b in range(17):
if i & 1 << b:
val.append([1 << b, i])
break
val.sort(reverse=True)
ans = []
for i in val:
if i[0] <= s:
s -= i[0]
ans.append(i[1])
if s > 0:
print(-1)
quit()
print(len(ans))
for i in ans:
print(i, end=" ") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR LIST BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | s, limit = [int(c) for c in input().split()]
def lowbit(a):
res = 1
while a % 2 == 0:
res += 1
a = a // 2
return res
a = [0] * 20
examples = [[] for _ in range(20)]
for i in range(1, limit + 1):
a[lowbit(i)] += 1
examples[lowbit(i)].append(i)
length = -1
res = []
c = 1
while s != 0:
if s <= a[1]:
res = res + examples[1][:s]
length += s
s = 0
else:
max_pow = len("{0:b}".format(s))
while max_pow != 1 and a[max_pow] == 0:
max_pow -= 1
if max_pow == 1:
length = -2
s = 0
else:
s = s - 2 ** (max_pow - 1)
length += 1
ex = examples[max_pow].pop()
a[max_pow] -= 1
res.append(ex)
print(length + 1)
if length >= 0:
print(" ".join(map(str, res))) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | import sys
input = sys.stdin.readline
def multi_input():
return map(int, input().split())
def array_print(arr):
print(" ".join(map(str, arr)))
sum, n = multi_input()
a = [0] * (n + 1)
for i in range(1, n + 1):
if i % 2 != 0:
a[i] = 1
else:
a[i] = 2 * a[i // 2]
b = []
new_sum = flag = 0
for j in reversed(range(1, n + 1)):
if new_sum + a[j] <= sum:
new_sum += a[j]
b.append(j)
if new_sum == sum:
flag = 1
break
if flag == 0:
print(-1)
else:
print(len(b))
array_print(b) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | a, b = [int(x) for x in input().split()]
arr = []
for i in range(b, 0, -1):
curr = i
c = 0
while curr & 1 == 0:
c += 1
curr = curr >> 1
if 2**c <= a:
a -= 2**c
arr.append(i)
if a == 0:
print(len(arr))
print(*arr)
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | m, n = map(int, input().split())
l = []
for i in range(1, n + 1):
x = bin(i)
p = x.split("1")
l.append((2 ** len(p[-1]), i))
l.sort(reverse=True)
i = 0
ans = []
while m > 0 and i < len(l):
if m >= l[i][0]:
m -= l[i][0]
ans.append(l[i][1])
i += 1
if m != 0:
print(-1)
exit()
print(len(ans))
print(*ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | def lowbit(n):
binary_n = bin(n).replace("0b", "")
last_1 = 0
for i, v in enumerate(binary_n[::-1]):
if v == "1":
last_1 = len(binary_n) - i - 1
break
lowbit_n = binary_n[last_1:]
return int(lowbit_n, 2)
summ, limit = list(map(int, input().split()))
ans = []
for i in range(limit, 0, -1):
if i % 2 == 0 and summ > 0:
if lowbit(i) <= summ:
ans.append(i)
summ -= lowbit(i)
for i in range(1, limit + 1):
if i % 2 != 0 and summ > 0:
ans.append(i)
summ -= 1
if summ == 0:
print(len(ans))
print(*ans)
else:
print(-1) | FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | s, l = map(int, input().split())
arr = [0] + [(0) for i in range(l)]
dic = {}
for i in range(1, l + 1):
x = bin(i)
m = len(x)
for j in range(0, m):
if x[m - j - 1] == "1":
arr[i] = 2**j
y = 2**j
if y in dic:
dic[y].append(i)
else:
dic[y] = [i]
break
f = s
arr1 = sorted(arr)
for i in range(l, 0, -1):
if arr1[i] > f:
arr1[i] = 0
else:
f += -arr1[i]
if sum(arr1) == s:
final = True
else:
final = False
final_arr = []
if final:
t = 0
for i in range(1, l + 1):
if arr1[i] != 0:
t += 1
for j in range(len(dic[arr1[i]])):
if dic[arr1[i]][j] != -1:
final_arr.append(dic[arr1[i]][j])
dic[arr1[i]][j] = -1
break
print(t)
for i in final_arr:
print(i, end=" ")
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER STRING ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | s, l = map(int, input().split())
lis = []
t = 2
count = l
while True:
if l // t != 0:
lis.append(l // t)
count += l // t * t // 2
t = t * 2
else:
break
if count < s:
print(-1)
else:
count = 0
t = 2 ** len(lis)
a = 2 ** len(lis)
k = t
lis = []
while t > 1:
if count + t <= s and k <= l:
count += t
lis.append(k)
if t == a:
k += t
else:
k += 2 * t
else:
t = t // 2
k = t
for i in range(1, l + 1, 2):
if count == s:
break
count += 1
lis.append(i)
print(len(lis))
print(*lis) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | s, l = map(int, input().split())
arr = sorted([(i & -i, i) for i in range(1, l + 1)])
ans = []
for c, i in arr[::-1]:
if s == 0:
break
if c <= s:
ans.append(i)
s -= c
if s:
print("-1")
else:
print(len(ans))
print(*ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | s, l = map(int, input().split())
d = [list() for _ in range(l)]
ans = list()
for i in range(l):
d[i] = [int("1" + "0" * bin(i + 1)[2:][::-1].index("1"), 2), i + 1]
for v in reversed(sorted(d)):
if v[0] <= s:
s -= v[0]
ans.append(str(v[1]))
if s > 0:
print(-1)
else:
print(len(ans))
print(" ".join(ans)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST FUNC_CALL VAR BIN_OP STRING BIN_OP STRING FUNC_CALL FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER STRING NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | sum, lim = list(map(int, input().split()))
a = []
while lim > 0 and sum > 0:
if lim & -lim <= sum:
sum = sum - (lim & -lim)
a.append(lim)
lim = lim - 1
if sum > 0:
print(-1)
else:
print(len(a))
for i in range(len(a)):
print(a[i], end=" ") | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | sum, limit = map(int, input().split())
def lowbit(k):
count = 0
for i in k[::-1]:
if i == "1":
return 2**count
else:
count += 1
actual = 0
max = 0
for i in range(1, limit + 1):
c = bin(i)[2:]
if lowbit(c) > max:
max = lowbit(c)
actual += lowbit(c)
if sum <= actual:
ans = []
count = 0
for i in range(limit, 0, -1):
count += lowbit(bin(i)[2:])
ans.append(i)
if count > sum:
count -= lowbit(bin(i)[2:])
ans.pop(-1)
print(len(ans))
print(*ans)
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR STRING RETURN BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | [S, L] = [int(i) for i in input().split()]
d = {}
for i in range(1, L + 1):
if i % 2 == 1:
d[i] = 1
else:
d[i] = 0
v = i
p = 0
while v % 2 == 0:
p += 1
v /= 2
d[i] = pow(2, p)
d = {k: v for k, v in sorted(d.items(), key=lambda item: item[1], reverse=True)}
total = 0
st = []
for k, v in d.items():
if v + total <= S:
st.append(k)
total += v
if total == S:
break
if total == S:
print(len(st))
for i in st:
print(str(i), end=" ")
else:
print(-1) | ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | (a, b), x, y = list(map(int, input().split())), [], 0
def bin(s):
return str(s) if s <= 1 else bin(s >> 1) + str(s & 1)
def lowbit(s):
return int("1" + bin(s).split("1")[-1], 2)
for i in reversed(range(b + 1)):
if y == a:
break
if a >= y + lowbit(i):
x.append(i)
y += lowbit(i)
if y == a:
print(len(x))
print(" ".join(str(i) for i in x))
else:
print(-1) | ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER FUNC_DEF RETURN VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR BIN_OP STRING FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | sum, limit = map(int, input().split(" "))
ans = set()
def low_bit(n):
k = 0
while not n & 1 << k:
k += 1
return 1 << k
for i in reversed(range(1, limit + 1)):
t = low_bit(i)
if sum >= t:
sum -= t
ans.add(i)
if sum == 0:
print(len(ans))
print(" ".join([str(x) for x in ans]))
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER RETURN BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | sum, limit = map(int, input().split())
l = []
for i in range(limit, 0, -1):
c = bin(i)[2:]
c = c[::-1]
index = c.index("1")
if sum - 2**index >= 0:
sum = sum - 2**index
l.append(i)
if sum == 0:
break
if sum == 0:
print(len(l))
print(*l)
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | def helper(n):
i = 0
while n > 0:
if n & 1 == 1:
return 2**i
i += 1
n = n >> 1
su, limit = map(int, input().split())
l = [0] * (limit + 1)
tsum = 0
for i in range(1, limit + 1):
if i % 2 == 0:
tempi = helper(i)
l[i] = tempi
tsum += tempi
else:
l[i] = 1
tsum += 1
if su > tsum:
print(-1)
else:
ans = []
tempsum = su
index = limit
while index >= 0 and tempsum > 0:
if l[index] == su:
ans = []
ans.append(index)
break
if l[index] <= tempsum:
ans.append(index)
tempsum -= l[index]
if tempsum <= 0:
break
index -= 1
print(len(ans))
for i in ans:
print(i, end=" ")
print() | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | s, l = map(int, input().split())
lowbit = [0]
idx = [0]
for i in range(1, l + 1):
x = bin(i)
x = x[::-1]
lowbit.append(2 ** x.index("1"))
idx.append(i)
idx.sort(key=lambda i: lowbit[i], reverse=True)
p = 0
ans = []
while s and p < l:
if lowbit[idx[p]] <= s:
s -= lowbit[idx[p]]
ans.append(idx[p])
p += 1
if s:
print(-1)
else:
print(len(ans))
print(*ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
def in_int():
return int(input())
def in_list():
return list(map(int, input().split()))
def in_str():
s = input()
return list(s[: len(s) - 1])
def in_ints():
return map(int, input().split())
total, n = in_ints()
l = [i for i in range(1, n + 1)]
l = sorted(l, key=lambda l: l & -l)
l = l[::-1]
ans = []
for xx in l:
tt = xx & -xx
if total >= tt:
total -= tt
ans.append(xx)
if total == 0:
print(len(ans))
for xx in ans:
print(xx, end=" ")
else:
print(-1) | IMPORT ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | s, l = map(int, input().split())
lis = [list() for _ in range(100001)]
for i in range(1, l + 1):
lis[i & ~(i - 1)].append(i)
num = int(pow(2, s.bit_length() - 1))
invalid = 1
ans = list()
while num:
if num & s:
temp = num
length = 1
while temp:
while len(lis[temp]):
if length == 0:
break
ans.append(lis[temp].pop())
length -= 1
length = length << 1
temp >>= 1
if length:
invalid = 0
num >>= 1
if invalid:
print(len(ans))
for var in ans:
print(var, end=" ")
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR IF BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR WHILE FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | s, l = map(int, input().split())
ct_limit = {i: [] for i in range(19)}
for i in range(1, l + 1):
cp = i
ct = 0
while i:
if i & 1:
ct_limit[ct].append(cp)
break
ct += 1
i >>= 1
if sum(2**k * len(v) for k, v in ct_limit.items()) < s:
print(-1)
else:
ans = []
ct = 0
for i in range(18, -1, -1):
sh = 0
while sh < len(ct_limit[i]) and s >= 2**i:
ans.append(ct_limit[i][sh])
sh += 1
s -= 2**i
if s != 0:
print(-1)
else:
print(len(ans))
print(*ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | sum, limit = input().split()
sum = int(sum)
limit = int(limit)
prev_sum = sum
s = []
cur_sum = 0
if sum % 2:
s.append(1)
sum -= 1
cur_sum += 1
count = {}
j = 0
while sum > 0:
if sum % 2:
count[2**j] = 1
sum = sum >> 1
j += 1
occur = [(0) for i in range(limit + 1)]
while len(count) > 0:
cur = next(iter(count))
if cur == 1:
i = 1
if prev_sum % 2:
i = 3
while count[cur] > 0 and i <= limit:
s.append(i)
cur_sum += 1
i += 2
count[cur] -= 1
elif cur > limit:
try:
count[cur // 2] += 2 * count[cur]
except:
count[cur // 2] = 2 * count[cur]
else:
i = 1
while count[cur] > 0 and i * cur <= limit:
while cur * i <= limit and occur[cur * i]:
i += 2
if cur * i <= limit:
s.append(cur * i)
occur[cur * i] += 1
cur_sum += cur
count[cur] -= 1
if count[cur] != 0:
try:
count[cur // 2] += 2 * count[cur]
except:
count[cur // 2] = 2 * count[cur]
del count[cur]
if cur_sum == prev_sum:
print(len(s))
for i in s:
print(i, end=" ")
print()
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER BIN_OP VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | s, limit = map(int, input().split())
Cnt = [[]]
for i in range(10**5):
Cnt.append([])
Ind = [0] * (10**5 + 1)
m = 0
for i in range(1, limit + 1):
pos = 0
x = i
while x % 2 == 0:
x //= 2
pos += 1
Cnt[2**pos].append(i)
m = max(m, 2**pos)
L = []
while s > 0:
e = s
for j in range(min(m, s), -1, -1):
if Ind[j] < len(Cnt[j]):
L.append(Cnt[j][Ind[j]])
Ind[j] += 1
s -= j
m = min(m, j)
break
if s == e:
print(-1)
break
if s == 0:
print(len(L))
for item in L:
print(item, end=" ") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | kk = lambda: map(int, input().split())
su, l = kk()
cnts, i = [], 0
while 1:
v = l // 2**i
v = v // 2 + (v & 1)
if v == 0:
break
cnts.append(v)
i += 1
c = 0
s = []
for i in range(len(cnts) - 1, -1, -1):
v = 2**i
while v <= l:
if c + 2**i > su:
break
s.append(v)
c += 2**i
v += 2 ** (i + 1)
if c < su:
print(-1)
else:
print(len(s))
print(*s) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | a, b = map(int, input().split())
res = []
for i in range(b, 0, -1):
k = i & (i ^ i - 1)
if a >= k:
res.append(str(i))
a -= k
if a > 0:
print(-1)
else:
print(len(res))
print(*res) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | def mi():
return map(int, input().split())
s, l = mi()
r = []
for i in range(l, 0, -1):
if int(bin(i & -i), 2) <= s:
r.append(i)
s -= int(bin(i & -i), 2)
if s:
print(-1)
else:
print(len(r))
print(*r) | FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | a = [(0) for x in range(0, 18)]
k = [(0) for x in range(0, 18)]
ss = input()
kay = ss.split(" ")
suma = int(kay[0])
limit = int(kay[1])
for i in range(16, -1, -1):
a[i] = int(limit / pow(2, i)) - int(limit / pow(2, i + 1))
for i in range(16, -1, -1):
while suma >= pow(2, i) and a[i] > 0:
suma = suma - pow(2, i)
a[i] = a[i] - 1
k[i] = k[i] + 1
les = ""
N = sum(k)
if suma != 0:
print(-1)
else:
print(N)
for i in range(0, 17):
j = 0
while k[i] != 0:
les = les + str(pow(2, i) * (2 * j + 1)) + " "
k[i] = k[i] - 1
j = j + 1
print(les) | ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | from sys import stdin
def arr_inp():
return [int(x) for x in stdin.readline().split()]
all, ans = [], []
sum, limit = arr_inp()
for i in range(1, limit + 1):
if i & 1:
all.append([i, 1])
else:
c, num = 1, i
while True:
num >>= 1
if num & 1:
all.append([i, 2**c])
break
c += 1
all.sort(key=lambda x: x[1])
for i in range(limit - 1, -1, -1):
if all[i][1] <= sum:
ans.append(all[i][0])
sum -= all[i][1]
if sum == 0:
print(len(ans))
exit(print(*ans))
print(-1) | FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE NUMBER VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | sum, lim = map(int, input().split())
arr = []
now = 0
for i in range(lim, -1, -1):
if now + (i & -i) <= sum:
now += i & -i
arr.append(i)
if now != sum:
print(-1)
else:
print(len(arr) - 1)
for i in range(0, len(arr) - 1):
print(arr[i]) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | def to_list(s):
return list(map(lambda x: int(x), s.split(" ")))
def solve(sum_, limit):
if limit == 1:
if sum_ > 1:
return "-1"
else:
return "1", "1"
else:
if limit % 2 == 0:
ones = limit / 2
else:
ones = int(limit / 2) + 1
if sum_ <= ones:
arr = list(range(1, sum_ * 2, 2))
s = " ".join(list(map(lambda x: str(x), arr)))
return str(len(arr)), s
else:
a = []
for i in range(1, limit + 1):
a.append(2 ** bin(i)[::-1].index("1"))
cifers = list(range(1, limit + 1))
zip_a = sorted(zip(a, cifers), key=lambda x: x[0], reverse=True)
a = [item[0] for item in zip_a]
cifers = [item[1] for item in zip_a]
even_idxs = []
for i, item in enumerate(a):
if item % 2 == 0:
even_idxs.append(i)
numbers = []
for even_idx in even_idxs:
sum_ -= a[even_idx]
numbers.append(str(cifers[even_idx]))
if sum_ <= ones:
arr = list(map(lambda x: str(x), list(range(1, sum_ * 2, 2))))
arr.extend(numbers)
s = " ".join(arr)
return str(len(arr)), s
return "-1"
sum_, limit = to_list(input())
answer = solve(sum_, limit)
if answer == "-1":
print("-1")
else:
print("\n".join(answer)) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN STRING RETURN STRING STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | n, l = map(int, input().split())
i = 1
while 2 * i < n:
i *= 2
res = []
while n and i:
for j in range(i, l + 1, 2 * i):
if i > n:
break
else:
res.append(j)
n -= i
i //= 2
if n == 0:
print(len(res))
print(*res)
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | s, l = map(int, input().split(" "))
li = []
while l != 0 and s != 0:
x = 1
if l % 2 == 0:
g = l
while g % 2 == 0:
x += 1
g = g >> 1
if s >= pow(2, x - 1):
s -= pow(2, x - 1)
li.append(l)
l -= 1
if s == 0:
print(len(li))
print(*li)
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST WHILE VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | def check(num):
x = str(bin(num))
j = -1
for i in range(len(x) - 1, -1, -1):
j += 1
if x[i] == "1":
break
return j
s, limit = [int(x) for x in input().split()]
res = []
for i in range(1, limit + 1):
x = check(i)
res.append([2**x, i])
res.sort(key=lambda x: x[0])
ans = []
for i in range(len(res) - 1, -1, -1):
if s >= res[i][0]:
s -= res[i][0]
ans.append(res[i][1])
if s == 0:
break
if s != 0:
print(-1)
else:
print(len(ans))
print(*ans, sep=" ") | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR NUMBER IF VAR VAR STRING RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | s, l = map(int, input().split())
r = []
while l > 0 and s > 0:
if s - (l & l) >= 0:
s -= l & -l
r.append(l)
l -= 1
if s != 0:
print(-1)
else:
print(len(r))
print(*r) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | def lowbit(x):
i = 0
while x & 1 == 0:
x >>= 1
i += 1
return 1 << i
def main():
Sum, limit = [int(i) for i in input().split()]
d = {}
for i in range(1, limit + 1):
tmp = lowbit(i)
if tmp not in d:
d[tmp] = []
d[tmp].append(i)
result = []
i = Sum
while i > 0 and Sum:
if i in d and d[i]:
result.append(d[i].pop())
Sum -= i
i = min(Sum, i)
else:
i -= 1
if Sum:
print("-1")
else:
print(len(result))
for i in result:
print(i, end=" ")
main() | FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | def main():
_sum, limit = map(int, input().split())
cur_sum = 0
cur_div = limit
ans = []
while cur_sum <= _sum and cur_div >= 1:
bin_div = bin(cur_div)[2:]
lowest1 = bin_div.rfind("1")
contr = 2 ** (len(bin_div) - lowest1 - 1)
assert contr == cur_div & -cur_div
if cur_sum + contr <= _sum:
cur_sum += contr
ans.append(cur_div)
cur_div -= 1
if cur_sum == _sum:
print(len(ans))
print(*ans)
else:
print(-1)
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | target, limit = list(map(int, input().split()))
def lowbit(x):
return x & (x ^ x - 1)
ans = []
for i in range(limit, 0, -1):
x = lowbit(i)
if x <= target:
ans.append(i)
target -= x
if target:
print(-1)
else:
print(len(ans))
print(" ".join(map(str, ans))) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | s, limit = map(int, input().split())
ans = []
for i in range(limit, 0, -1):
if s - (i & -i) >= 0:
ans.append(i)
s -= i & -i
if s == 0:
print(len(ans))
print(" ".join(map(str, ans)))
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | def findOrder(limit):
order = 0
while limit > 1:
limit = limit >> 1
order += 1
return order
def findLimit(num, limit):
order = findOrder(limit)
ans = []
while num != 0:
if order == -1:
return [-1]
multiplier = 1
base = 2**order
curNum = base * multiplier
while curNum <= limit and base <= num:
ans.append(curNum)
num -= base
multiplier += 2
curNum = base * multiplier
order -= 1
return ans
def printAnswer(lst):
if lst[0] == -1:
print(-1)
else:
print(len(lst))
for i in lst:
print(i, end=" ")
sm, lmt = map(int, input().split(" "))
printAnswer(findLimit(sm, lmt)) | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR NUMBER IF VAR NUMBER RETURN LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | s, n = list(map(int, input().split()))
y = []
y1 = []
sum1 = 0
for i in range(1, n + 1):
power = 0
de = i
while i % 2 == 0:
power += 1
i = i // 2
if sum1 + 2**power <= s:
sum1 += 2**power
y.append(de)
sum2 = 0
for i in range(n, 0, -1):
power = 0
de = i
while i % 2 == 0:
power += 1
i = i // 2
if sum2 + 2**power <= s:
sum2 += 2**power
y1.append(de)
if sum1 == s:
print(len(y))
print(*y)
elif sum2 == s:
print(len(y1))
print(*y1)
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | def lowval(i):
global low
return low[i]
s, lim = list(map(int, input().split()))
low = [0]
result = []
m, pm = 1, 1
while len(low) - 1 < lim:
pm = len(low) - 1
low += low[1 : len(low) - 1] + [m]
m *= 2
else:
if len(low) - 1 == lim:
pm = len(low) - 1
d = list(range(lim + 1))
x = sorted(d, key=lowval)
x2 = list(map(lowval, x))
for i in range(lim, 0, -1):
if x2[i] <= s:
result.append(x[i])
s -= x2[i]
if s == 0:
break
if s != 0:
print(-1)
else:
print(len(result))
for i in result:
print(i, end=" ")
print() | FUNC_DEF RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER WHILE BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER LIST VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | from sys import stdin, stdout
nmbr = lambda: int(stdin.readline())
lst = lambda: list(map(int, input().split()))
p2 = [1]
for i in range(18):
p2 += [p2[-1] * 2]
N = 10**5 + 5
lb = [0]
for i in range(1, N):
for bp in range(18):
if i >> bp & 1:
pos = bp
break
lb += [p2[pos]]
for i in range(1):
sm, n = lst()
v = n
ans = set()
while v > 0:
if v <= sm:
sm -= lb[v]
ans.add(v)
v -= 1
if sm == 0:
break
if sm != 0:
print(-1)
else:
print(len(ans))
for v in ans:
stdout.write(str(v) + " ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR LIST BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | sum, limit = [int(x) for x in input().split()]
odd = int(limit / 2)
even = odd
if limit % 2 == 1:
odd += 1
a = []
for i in range(2, limit + 1, 2):
c = 0
num = i
while i % 2 == 0:
i /= 2
c += 1
a.append([int(2**c), int(num)])
a.sort(key=lambda x: x[0], reverse=True)
x = len(a)
s = 0
i = 0
ans = []
count = 0
while i != even and s + a[i][0] < sum:
s += a[i][0]
ans.append(a[i][1])
count += 1
i += 1
if s == sum:
print(count)
print(*ans)
else:
rem = sum - s
if odd < rem:
print(-1)
else:
j = 1
for i in range(rem):
ans.append(j)
j += 2
count += 1
print(count)
print(*ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | sum, limit = map(int, input().split())
ans = []
for i in range(limit, 0, -1):
n = i & -i
if n <= sum:
sum -= n
ans.append(i)
if sum == 0:
print(len(ans))
for i in ans:
print(i, end=" ")
else:
print(-1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | class IOHandlerObject(object):
def getInput(self, mode=2):
inputs = input().strip()
if mode == 0:
return inputs
if mode == 1:
return inputs.split()
if mode == 2:
return [int(x) for x in inputs.split()]
def writeOutput(self, s="\n"):
if isinstance(s, list):
s = " ".join(s)
print(s)
IOHandler = IOHandlerObject()
g = IOHandler.getInput
w = IOHandler.writeOutput
s, l = g()
res = []
i = 16
while i + 1:
c = 1
while True:
if 2**i * c > l:
break
if 2**i > s:
break
res.append(2**i * c)
c += 2
s -= 2**i
i -= 1
if s:
print(-1)
else:
print(len(res))
print(" ".join(str(x) for x in res)) | CLASS_DEF VAR FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_DEF STRING IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | def low(n):
x = str(bin(n))[2:]
x = x[::-1]
t = x.index("1")
temp = x[: t + 1][::-1]
return int(temp, 2)
def child_set(s, limit):
lst = [[i, low(i)] for i in range(1, limit + 1)]
lst = [[0, 0]] + lst
lst = sorted(lst, reverse=True, key=lambda s: s[1])
i = 0
ans = []
while s and lst and i < len(lst):
if lst[i][1] <= s:
s -= lst[i][1]
ans.append(lst[i][0])
lst.pop(i)
else:
i += 1
if s:
return -1
else:
print(len(ans))
print(*ans)
return ""
a, b = map(int, input().strip().split())
print(child_set(a, b)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | S, L = map(int, input().split())
d, r = {}, {}
k = 0
while L >= 2**k:
d[2**k] = (L + 2**k) // 2 ** (k + 1)
k += 1
for i in sorted(d.keys())[::-1]:
while i <= S and d[i]:
S -= i
d[i] -= 1
r[i] = r.get(i, 0) + 1
ans = []
for i in sorted(r.keys())[::-1]:
for j in range(d[i] + r[i] - 1, d[i] - 1, -1):
ans += [str((2 * j + 1) * i)]
if S:
print(-1)
else:
print(len(ans))
print(" ".join(ans)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR DICT DICT ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR LIST FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | def main():
s, lm = map(int, input().split())
c = {}
for i in range(1, lm + 1):
if i & -i not in c:
c[i & -i] = 1
else:
c[i & -i] += 1
p = list(c.keys())
p.sort(reverse=True)
used = {k: (0) for k in p}
total = 0
for k in p:
count = 0
if s >= k and count < c[k]:
while s >= k and count < c[k]:
count += 1
s -= k
used[k] = count
total += count
if s > 0:
print(-1)
else:
print(total)
ans = []
for i in range(1, lm + 1):
if used[i & -i] > 0:
ans.append(i)
used[i & -i] -= 1
print(*ans)
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\sum_{x \in S} \text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
-----Input-----
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 10^5).
-----Output-----
In the first line print an integer n (1 ≤ n ≤ 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
-----Examples-----
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
-----Note-----
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4. | __author__ = "asmn"
s, l = tuple(map(int, input().split(" ")))
t = 1
while t << 1 <= s:
t <<= 1
ans = []
while s > 0 and t > 0:
n = min((l // t + 1) // 2, s // t)
s -= t * n
for i in range(n):
ans.append((i * 2 + 1) * t)
t >>= 1
if s > 0:
print(-1)
else:
print(len(ans))
print(*tuple(ans)) | ASSIGN VAR STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.