description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
s = list(map(int, input().split()))
def chk(l, r):
if len(r) == 0:
print(" ".join(map(str, l)))
exit()
ll = l.copy()
rr = r.copy()
if l[-1] % 3 == 0 and l[-1] // 3 in rr:
rr.remove(l[-1] // 3)
chk(ll + [l[-1] // 3], rr)
rr = r.copy()
if l[-1] * 2 in rr:
rr.remove(l[-1] * 2)
chk(ll + [l[-1] * 2], rr)
for x in s:
remain = s.copy()
remain.remove(x)
chk([x], remain)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST BIN_OP VAR NUMBER NUMBER VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
import sys
n = int(input())
a = [*map(int, input().split())]
def solve(c, r, s, x):
sys.setrecursionlimit(sys.getrecursionlimit() + 1)
r.remove(x)
s.remove(x)
c.append(x)
if len(r) == 0:
print(*c)
exit()
if x % 3 == 0 and x // 3 in s:
solve([*c], [*r], set([*s]), x // 3)
if x * 2 in s:
solve([*c], [*r], set([*s]), x * 2)
return
for i in a:
solve([], [*a], set(a), i)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR LIST VAR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR LIST VAR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER RETURN FOR VAR VAR EXPR FUNC_CALL VAR LIST LIST VAR FUNC_CALL VAR VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def Alfa(a):
m.append(a)
if a % 3 == 0 and a // 3 in z:
z.remove(a)
Alfa(a // 3)
elif a * 2 in z:
z.remove(a)
Alfa(a * 2)
elif len(m) == n:
for i in m:
print(i, end=" ")
p = []
n = int(input())
k = sorted(map(int, input().split()))
k = k[::-1]
d = []
for i in k:
m = []
z = k.copy()
Alfa(i)
|
FUNC_DEF EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
import sys
input = sys.stdin.readline
N = int(input())
S = list(map(int, input().split()))
def neighbours(node, s=S):
out = [node << 1]
if node % 3 == 0:
out.append(node // 3)
return set(x for x in out if x in s)
G = {}
for x in S:
G[x] = neighbours(x)
def hamilton(G, size, pt, path=None):
if path == None:
path = []
if pt not in set(path):
path.append(pt)
if len(path) == size:
return path
for pt_next in G.get(pt, []):
res_path = [i for i in path]
candidate = hamilton(G, size, pt_next, res_path)
if candidate is not None:
return candidate
for x in S:
P = hamilton(G, N, x)
if P:
print(" ".join(map(str, P)))
break
|
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 FUNC_DEF VAR ASSIGN VAR LIST BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_DEF NONE IF VAR NONE ASSIGN VAR LIST IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR LIST ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NONE RETURN VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = [int(x) for x in input().split()]
b = []
for i in range(n):
if a[i] > 100000000:
if a[i] * 3 not in a and a[i] // 2 not in a:
b.append(a[i])
a.pop(i)
break
elif a[i] * 3 not in a and a[i] / 2 not in a:
b.append(a[i])
a.pop(i)
break
i = 0
while i < len(a):
if a[i] == b[-1] * 2 or a[i] == b[-1] // 3:
b.append(a[i])
a.pop(i)
i = 0
else:
i += 1
b = [str(x) for x in b]
print(" ".join(b))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def main():
n = int(input())
a = list(map(int, input().split(" ")))
for item in a:
current = item
copy = list(a)
sequence = [current]
copy.remove(current)
while copy:
divided = current // 3
multiplied = current * 2
if divided in copy and current % 3 == 0:
copy.remove(divided)
sequence.append(divided)
current = divided
elif multiplied in copy:
copy.remove(multiplied)
sequence.append(multiplied)
current = multiplied
else:
break
if set(sequence) == set(a):
return " ".join(map(str, sequence))
print(main())
|
FUNC_DEF 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 VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = eval(input())
a = input().split()
length = n
start = 0
for i in a:
if int(i) % 2 == 0:
if not str(eval(i) * 3) in a and not str(eval(i) // 2) in a:
start = eval(i)
break
elif not str(eval(i) * 3) in a:
start = eval(i)
break
print(start, end=" ")
while length:
length -= 1
if start % 2 == 0:
if start % 3 == 0:
x = start // 3
if str(x) in a:
start = x
print(start, end=" ")
continue
x = start * 2
if str(x) in a:
print(x, end=" ")
start = x
else:
x = start * 2
if str(x) in a:
print(x, end=" ")
start = x
continue
y = start // 3
if str(y) in a:
print(y, end=" ")
start = y
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING WHILE VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = list(map(int, input().split()))
g = [([0] * n) for _ in range(n)]
for i in range(n):
for j in range(n):
if a[i] * 2 == a[j]:
g[i][j] = 1
if a[j] * 3 == a[i]:
g[i][j] = 1
def dfs(u, b):
for v in range(n):
if g[u][v]:
if dfs(v, b + [a[v]]):
return True
if len(b) == n:
print(*b)
return True
return False
for u in range(n):
if dfs(u, [a[u]]):
break
|
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 VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR LIST VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
s = list(map(int, input().split()))
result = []
i = 0
def checkeven(a):
if a % 2 == 0:
return True
else:
return False
def check(a):
b = 0
if checkeven(a) is True:
if a // 2 in s:
b += 1
if a * 2 in s:
b += 10
if a * 3 in s:
b += 1
if a / 3 in s:
b += 10
elif checkeven(a) is False:
if a / 2 in s:
b += 1
if a * 2 in s:
b += 10
if a * 3 in s:
b += 1
if a // 3 in s:
b += 10
if b == 10 or b == 0:
return True
while i < n and n != 0:
if check(s[i]) is True:
result.append(s[i])
s.remove(s[i])
i = 0
n -= 1
else:
i += 1
for k in range(len(result)):
print(result[k], end=" ")
|
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 ASSIGN VAR NUMBER FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER WHILE VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def helper(curr, ans, l, visited, n):
if l == n:
for i in ans:
print(i, end=" ")
return True
for i in arr:
if i not in visited and (i * 3 == curr or i == curr * 2):
visited.add(i)
if helper(i, ans + [i], l + 1, visited, n):
return True
visited.remove(i)
return False
n = int(input())
arr = [int(x) for x in input().split()]
for i in arr:
v = set()
v.add(i)
if helper(i, [i], 1, v, n):
break
|
FUNC_DEF IF VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING RETURN NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR LIST VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR LIST VAR NUMBER VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def main():
n = int(input())
a = [int(x) for x in input().split()]
b = set(a)
for e in a:
ans = [e]
b = set(a)
b.remove(e)
cur = e
flag = True
while flag and len(b):
if cur % 3 == 0 and cur // 3 in b:
cur = cur // 3
ans.append(cur)
b.remove(cur)
elif cur * 2 in b:
cur = cur * 2
ans.append(cur)
b.remove(cur)
else:
flag = False
if flag:
print(" ".join(str(x) for x in ans))
break
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def DFS(n, v):
global num, pos, ans
pos[v] = True
for i in range(n):
if num[v][i] != 0 and not pos[i]:
if len(ans) == 0:
ans += str(spis[v]) + " " + str(spis[i]) + " "
else:
ans += str(spis[i]) + " "
DFS(n, i)
n = int(input())
spis = list(map(int, input().split()))
num = [[(0) for j in range(n)] for i in range(n)]
pos = [(False) for i in range(n)]
ans = ""
for i in range(n):
if spis[i] * 2 in spis:
num[i][spis.index(spis[i] * 2)] = 1
if spis[i] % 3 == 0 and spis[i] // 3 in spis:
num[i][spis.index(spis[i] // 3)] = 1
for i in range(n):
if False in pos and True in pos:
pos = [(False) for j in range(n)]
if False not in pos:
break
ans = ""
DFS(n, i)
print(ans)
|
FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR VAR STRING VAR BIN_OP FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR 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 VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR IF NUMBER VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input().strip())
nums = list(map(int, input().strip().split()))
visited = [0] * n
def dfs(i):
res = []
flag = True
for j in range(n):
if not visited[j]:
if nums[i] % 3 == 0 and nums[j] == nums[i] // 3 or nums[j] == nums[i] * 2:
flag = False
visited[j] = 1
tmp = dfs(j)
visited[j] = 0
if isinstance(tmp[0], list):
for item in tmp:
res.append([nums[i]] + item)
else:
res.append([nums[i]] + tmp)
if flag:
res = [nums[i]]
return res
for i in range(n):
visited[i] = 1
ans = dfs(i)
visited[i] = 0
if not isinstance(ans[0], list):
continue
for j in range(len(ans)):
cur = ans[j]
if len(cur) == n:
cur = list(map(str, cur))
print(" ".join(cur))
i = n + 1
break
|
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 VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR IF VAR ASSIGN VAR LIST VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP VAR NUMBER
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
I = lambda: list(map(int, input().split()))
def ch(n):
i = 0
while not n % 2:
i += 1
n //= 2
return -n, i
(n,) = I()
l = I()
l.sort(key=ch)
print(*l)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = list(map(int, input().split()))
g = [[] for i in range(n)]
for i in range(n):
for j in range(n):
if a[i] * 3 == a[j]:
g[j].append(i)
if a[i] == a[j] * 2:
g[j].append(i)
s = -1
for i in range(n):
if len(g[i]) == 0:
s = i
break
x = a[s]
l = [x]
for i in range(n - 1):
if x * 3 in a:
l.append(x * 3)
x = x * 3
else:
l.append(x // 2)
x = x // 2
print(*l[::-1])
|
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 VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def get_power(n, k):
p = 0
while n % k == 0:
n //= k
p += 1
return p
n = int(input())
a = [[int(n), get_power(int(n), 3), get_power(int(n), 2)] for n in input().split()]
a.sort(key=lambda e: e[2])
a.sort(key=lambda e: e[1], reverse=True)
print(" ".join([str(e[0]) for e in a]))
|
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR NUMBER VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def f(x, base):
out = 0
while x % base == 0:
x //= base
out += 1
return out
n = int(input())
L = list(map(lambda i: [-f(int(i), 3), int(i)], input().split()))
L.sort()
for i, j in L:
print(j, end=" ")
print()
|
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def main():
n, l = int(input()), []
for a in map(int, input().split()):
b = a
two = three = 0
while not a % 2:
a //= 2
two += 1
while not a % 3:
a //= 3
three -= 1
l.append((three, two, b))
print(*[t[2] for t in sorted(l)])
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER WHILE BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def f(n, A):
A = list(map(int, A.split()))
dct = [[0, 0, 0] for i in range(n)]
for i in range(n):
tmp = A[i]
while True:
if tmp % 3 == 0:
dct[i][0] += 1
tmp //= 3
elif tmp % 2 == 0:
dct[i][1] -= 1
tmp //= 2
else:
break
dct[i][2] = i
dct.sort(reverse=True)
res = []
for i in range(n):
res += [str(A[dct[i][2]])]
return " ".join(res)
a = int(input())
A = input()
print(f(a, A))
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = list(map(int, input().split()))
count = []
for i in range(n):
x = 1
c = 0
while a[i] % x == 0:
x *= 3
c += 1
c -= 1
count.append([-c, a[i]])
count.sort()
for i in range(n):
print(count[i][1], end=" ")
|
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 ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = list(map(int, input().split(" ")))
ar = []
def dfs(n):
if n % 3 == 0 and n // 3 in a:
ar.append(n // 3)
dfs(n // 3)
elif n * 2 in a:
ar.append(n * 2)
dfs(n * 2)
for i in range(0, len(a)):
dfs(a[i])
if len(ar) == len(a) - 1:
ar.insert(0, a[i])
break
del ar[:]
for i in range(0, len(ar)):
print(ar[i], end=" ")
|
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 LIST FUNC_DEF IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
import sys
sys.setrecursionlimit(1500)
n = int(input())
v = list(map(int, input().strip().split()))
visited = set()
counter = 0
def solve(curr, seq):
seq.append(curr)
visited.add(curr)
if len(seq) == n:
print(" ".join(map(str, seq)))
elif len(seq) <= n:
if curr * 2 in v and curr * 2 not in seq:
solve(curr * 2, seq)
if curr // 3 in v and curr % 3 == 0 and curr // 3 not in seq:
solve(curr // 3, seq)
while counter < n:
curr = v[counter]
counter += 1
if curr in visited:
continue
solve(curr, [])
|
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR LIST
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
import time
n = int(input())
a = [int(i) for i in input().split()]
start = time.time()
pdt = []
div = []
for i in range(n):
pdt.append(a[i] * 2)
if a[i] % 3 == 0:
div.append(a[i] // 3)
else:
div.append(0)
res = []
for i in range(n):
c = 0
for j in range(n):
if a[i] != pdt[j] and a[i] != div[j]:
c += 1
if c == n:
res.append(a[i])
for elem in res:
if elem * 2 in a:
res.append(elem * 2)
if elem // 3 in a and elem % 3 == 0:
res.append(elem // 3)
if len(res) == 100:
break
for elem in res:
print(elem, end=" ")
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
l = list(map(int, input().split()))
m = []
for i in range(n):
flag = 1
x = l[i]
for j in range(n):
if x == 2 * l[j] or x * 3 == l[j]:
flag = 0
break
else:
continue
if flag == 1:
m.append(x)
for i in range(n):
for j in range(n):
if l[j] * 3 == m[i] or l[j] == m[i] * 2:
m.append(l[j])
break
for i in range(n):
print(m[i], end=" ")
print()
|
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 ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = []
o = [[] for i in range(80)]
for i in a:
temp = int(str(i))
c = 0
while True:
if temp % 2 == 0:
temp /= 2
c += 1
else:
break
o[c].append(i)
f = []
for i in o:
f = f + sorted(i)[::-1]
print(*f)
|
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 LIST ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
b = a.copy()
r = []
r.append(b[i])
current = b.pop(i)
while b:
f = False
for j, el in enumerate(b):
if current % 3 == 0 and current // 3 == el or current * 2 == el:
r.append(el)
current = b.pop(j)
f = True
break
if not f:
break
if len(r) == len(a):
break
print(*r)
|
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 FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
class Graph:
def __init__(self, n):
self.n = n
self.adj = [[] for i in range(n)]
def add(self, v, u):
self.adj[v].append(u)
n = int(input())
g = Graph(n)
a = list(map(int, input().split()))
for i in range(n):
for j in range(n):
if a[i] == 2 * a[j] or 3 * a[i] == a[j]:
g.add(i, j)
def dfs(v):
x = []
used[v] = True
for u in g.adj[v]:
if used[u]:
continue
y = dfs(u)
if len(y) > len(x):
x = y
x.append(v)
return x
for i in range(n):
used = [(False) for i in range(g.n)]
ans = dfs(i)
if len(ans) == n:
break
for i in ans:
print(a[i], end=" ")
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
print(
*sorted(
list(map(int, __import__("sys").stdin.read().split("\n")[1].split())),
key=lambda x: (
(lambda f, x, y: f(f, x, y))(
lambda f, x, y: 0 if x == 0 or x % y != 0 else f(f, x // y, y) + 1, x, 2
),
-(lambda f, x, y: f(f, x, y))(
lambda f, x, y: 0 if x == 0 or x % y != 0 else f(f, x // y, y) + 1, x, 3
),
),
)
)
|
EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING NUMBER FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def count(num, k):
if num % k != 0:
return 0
return 1 + count(num // k, k)
n = int(input())
a = [*map(int, input().split())]
c = [None] * n
for i in range(0, n):
c[i] = i
a.sort(key=lambda num: (-count(num, 3), num))
print(" ".join(map(str, a)))
|
FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
tempdict = dict()
input()
for i in input().split():
i = int(i)
tempdict[i] = True
key = i
ans = str(key)
while True:
if key % 2 is 0 and tempdict.get(key // 2):
key = key // 2
ans = str(key) + " " + ans
continue
if tempdict.get(key * 3):
key = key * 3
ans = str(key) + " " + ans
continue
break
key = i
while True:
if key % 3 is 0 and tempdict.get(key // 3):
key = key // 3
ans = ans + " " + str(key)
continue
if tempdict.get(key * 2):
key = key * 2
ans = ans + " " + str(key)
continue
break
print(ans)
|
ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING VAR ASSIGN VAR VAR WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = list(map(int, input().split()))
def count3(n):
ret = 0
while n % 3 == 0:
ret += 1
n = n // 3
return ret
v = [[0, 0] for i in range(n)]
for i in range(len(a)):
v[i][1] = a[i]
v[i][0] = -count3(v[i][1])
v.sort()
for i in v:
print(i[1], end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
mod = 1000000007
ii = lambda: int(input())
si = lambda: input()
dgl = lambda: list(map(int, input()))
f = lambda: map(int, input().split())
il = lambda: list(map(int, input().split()))
ls = lambda: list(input())
def dfs(d, sv):
d[sv] = True
dfsarr.append(sv)
for i in d:
if d[i] == False and (sv * 2 == i or sv % 3 == 0 and sv // 3 == i):
dfs(d, i)
dfsarr = []
n = ii()
l = il()
d = dict()
for i in range(n):
dfsarr = []
for j in l:
d[j] = False
dfs(d, l[i])
if all(d[k] for k in d):
print(*dfsarr)
|
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = list(map(int, input().split()))
aa = []
for i in range(n):
tmp = a[i]
p2 = 0
while tmp % 2 == 0:
tmp //= 2
p2 += 1
p3 = 0
while tmp % 3 == 0:
tmp //= 3
p3 += 1
aa.append((p2 - p3, i))
aa.sort()
ans = []
for _, i in aa:
ans.append(a[i])
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 VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
from sys import stdin
def solve(el, input):
if el not in input:
return []
res1 = solve(el * 2, input)
res2 = solve(el // 3, input) if el % 3 == 0 else []
res = max([res1, res2], key=len)
return [el] + res
def threetwo(n, input):
for el in input:
result = solve(el, input)
if len(result) == n:
return result
n = int(stdin.readline())
input = [int(el) for el in stdin.readline().split()]
result = threetwo(n, set(input))
print(" ".join(str(i) for i in result))
|
FUNC_DEF IF VAR VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR LIST ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR RETURN BIN_OP LIST VAR VAR FUNC_DEF FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
arr = [(int(x), int(x)) for x in input().strip().split()]
ans = []
while len(ans) < n:
odds = []
evens = []
for x, y in arr:
if y % 2 == 1:
odds.append((x, y))
else:
evens.append((x, y // 2))
ans += [x for x, y in sorted(odds, key=lambda x: x[0], reverse=True)]
arr = evens
print(" ".join([str(x) for x in ans]))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
sequence = input().split()
sequence = [int(x) for x in sequence]
div3_array = [0] * n
def count3(number, idx, div3_array):
count = 0
while number % 3 == 0:
number //= 3
count += 1
div3_array[idx] = count
for idx, number in enumerate(sequence):
count3(number, idx, div3_array)
div3_array = list(reversed(sorted(zip(div3_array, sequence))))
div = div3_array[0][0]
start = 0
end = 0
temp_array = []
answer = []
for value in div3_array:
if value[0] == div:
end += 1
temp_array.append(value[1])
continue
else:
temp_array = list(reversed(temp_array))
answer.extend(temp_array)
div = value[0]
temp_array = [value[1]]
temp_array = list(reversed(temp_array))
answer.extend(temp_array)
answer = [str(x) for x in answer]
answer = " ".join(answer)
print(answer)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
def f(n):
r = 0
while n % 2 == 0:
n //= 2
r += 1
while n % 3 == 0:
n //= 3
r -= 1
return r
print(*sorted(map(int, input().split()), key=f))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def canTake(nbr, lst, dict):
return dict[nbr] != lst.count(nbr)
def exists(nbr, dict):
return nbr in dict
def dfs(nbr, lst, dict, n):
if len(lst) == n:
return lst
possibilities = []
if exists(nbr * 2, dict) and canTake(nbr * 2, lst, dict):
possibilities.append(nbr * 2)
if nbr % 3 == 0 and exists(nbr // 3, dict) and canTake(nbr // 3, lst, dict):
possibilities.append(nbr // 3)
if len(possibilities) == 0:
return []
res = []
for item in possibilities:
newLst = list(lst)
newLst.append(item)
res.append(dfs(item, newLst, dict, n))
return max(res, key=len)
def main():
n = int(input())
arr = map(int, input().split())
dict = {}
for item in arr:
if exists(item, dict):
dict[item] += 1
else:
dict[item] = 1
res = []
for item in dict:
res.append(dfs(item, [item], dict, n))
print(" ".join(str(x) for x in max(res, key=len)))
main()
|
FUNC_DEF RETURN VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR LIST IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
array = [int(i) for i in input().split()]
s = set()
for i in array:
s.add(i)
z = [0] * 300
i = 150
j = 151
x = array[0]
y = array[0]
z[i] = x
i -= 1
s.remove(x)
while len(s) != 0:
if x * 3 in s:
z[i] = int(x * 3)
s.remove(x * 3)
x = x * 3
i -= 1
if x // 2 in s and x % 2 == 0:
z[i] = int(x // 2)
s.remove(x // 2)
x = x // 2
i -= 1
if y // 3 in s and y % 3 == 0:
z[j] = int(y // 3)
s.remove(y // 3)
y = y // 3
j += 1
if y * 2 in s:
z[j] = int(y * 2)
s.remove(y * 2)
y = y * 2
j += 1
for i in z:
if i != 0:
print(i, end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def in_power(a):
ans = 0
while a % 3 == 0:
a = a // 3
ans += 1
return ans
def main():
n = int(input())
numbers = [int(i) for i in input().split()]
ans = [""] * n
for i in range(n):
ans[i] = -in_power(numbers[i]), numbers[i]
ans.sort()
for i in range(n):
print(ans[i][1], end=" ")
main()
|
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF 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 STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
arr = list(map(int, input().split()))
edge = []
for i in arr:
flag = 0
if i * 3 in arr:
edge.append([i * 3, i])
flag += 1
if i % 2 == 0 and i // 2 in arr:
edge.append([i // 2, i])
flag += 1
if flag == 0:
edge.append([-1, i])
x = arr.index(i)
l = len(edge)
temp = edge[x][1]
print(temp, end=" ")
for i in range(n - 1):
for j in range(l):
if edge[j][0] == temp:
print(edge[j][1], end=" ")
temp = edge[j][1]
break
|
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 VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER STRING ASSIGN VAR VAR VAR NUMBER
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
values = input()
values = values.split()
factor = [[]] * 101
for i in range(n):
number = int(values[i])
factor[i] = [number, 0]
temp_number = number
while temp_number % 3 == 0:
factor[i][1] += 1
temp_number //= 3
maximo = 0
curr_max = -1
str_ans = ""
j = 0
while j < n:
for i in range(n):
if factor[i][1] > maximo:
maximo = factor[i][1]
curr_max = int(factor[i][0])
if curr_max != -1:
arr_curr = [curr_max]
else:
arr_curr = []
for m in range(n):
if factor[m][1] == maximo and curr_max != int(factor[m][0]):
arr_curr.append(int(factor[m][0]))
j += 1
arr_curr = sorted(arr_curr)
for k in range(len(arr_curr)):
str_ans += str(arr_curr[k]) + " "
for l in range(n):
if factor[l][0] == arr_curr[k]:
factor[l][1] = -1
maximo = 0
curr_max = -1
j += 1
print(str_ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = list(map(int, input().split()))
def prime3_occurences(n):
cont = 0
while n % 3 == 0:
n = n // 3
cont += 1
return cont
tups = []
for el in a:
tups.append((prime3_occurences(el), el))
tups = sorted(tups, key=lambda x: (-x[0], x[1]))
a = [str(x[1]) for x in tups]
output = " ".join(a)
print(output)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
from sys import stdin, stdout
def rearrage_polycarp_backtracking(n: int, sequence):
poly = set(sequence)
def recurse(start: int, path=[], seen=set()) -> bool:
if start not in poly or start in seen:
return []
path.append(start)
seen.add(start)
if len(recurse(2 * start, path, seen)) == n:
return path
if start % 3 == 0 and len(recurse(start // 3, path, seen)) == n:
return path
if len(path) != n:
seen.remove(path.pop())
return path
i = 0
while i < n:
path = recurse(sequence[i], [], set())
if path:
return path
i += 1
def rearrage_polycarp_math_and_sorting_solution(n: int, sequence):
def exp(b: int, a: int) -> int:
k = 0
while a % b == 0:
k += 1
a //= b
return k
power_of_3 = [(-exp(3, num), num) for num in sequence]
power_of_3.sort()
return [num[1] for num in power_of_3]
def rearrage_polycarp(n: int, sequence):
polys_seq = set(sequence)
ordered_seq = [
num
for num in sequence
if not (num % 2 == 0 and num // 2 in polys_seq or 3 * num in polys_seq)
]
n -= 1
i = 0
while n:
if 2 * ordered_seq[i] in polys_seq:
ordered_seq.append(2 * ordered_seq[i])
else:
ordered_seq.append(ordered_seq[i] // 3)
n -= 1
i += 1
return ordered_seq
n = int(stdin.readline().rstrip())
sequence = [int(x) for x in stdin.readline().rstrip().split(" ")]
stdout.write(
" ".join(str(num) for num in rearrage_polycarp_backtracking(n, sequence)) + "\n"
)
|
FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF VAR LIST FUNC_CALL VAR IF VAR VAR VAR VAR RETURN LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR RETURN VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR IF VAR RETURN VAR VAR NUMBER FUNC_DEF VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR NUMBER VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = list(map(int, input().split()))
num = {}
for i in range(50):
num[i] = []
for i in range(n):
temp = a[i]
cnt = 0
while True:
if temp % 3 == 0:
cnt += 1
temp //= 3
else:
break
num[cnt].append(a[i])
ans = []
for i in range(50)[::-1]:
temp = num[i]
temp = sorted(temp)
for i in temp:
ans.append(i)
print(*ans, sep=" ")
|
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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = list(map(int, input().split()))
d = dict()
minP = 9999
maxP = 0
for x in a:
curr = x
p = 0
while curr % 3 == 0:
curr //= 3
p += 1
minP = min(minP, p)
maxP = max(maxP, p)
l = d.get(p, [])
l.append(x)
d[p] = l
for key in range(maxP, minP - 1, -1):
l = d[key]
l.sort()
if key > minP:
e = " "
else:
e = ""
print(" ".join(map(str, l)), sep=" ", end=e)
|
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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = list(map(int, input().split()))
x = 0
r = []
for i in range(n):
ok = True
r = []
x = a[i]
r.append(x)
while ok:
ok = False
if x % 3 == 0:
if x // 3 in a:
ok = True
x = x // 3
r.append(x)
if x * 2 in a:
ok = True
x = x * 2
r.append(x)
if len(r) == n:
break
print(" ".join(map(str, r)))
|
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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def key(d, n):
for i in d:
if d[i] == n:
return i
return -1
n = int(input())
l = [int(i) for i in input().split()]
d = {}
for i in range(n):
if l[i] % 3 == 0:
d[l[i]] = [l[i] // 3, l[i] * 2]
else:
d[l[i]] = [l[i] * 2]
for i in d:
c = 1
for j in d[i]:
if j in d:
c = 0
d[i] = j
if c == 1:
d[i] = []
k = i
l1 = [k]
while key(d, k) != -1:
k1 = key(d, k)
l1.append(k1)
k = k1
print(*l1[::-1])
|
FUNC_DEF FOR VAR VAR IF VAR VAR VAR RETURN VAR RETURN NUMBER 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 VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR LIST BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR LIST BIN_OP VAR VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR LIST ASSIGN VAR VAR ASSIGN VAR LIST VAR WHILE FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def cmp(x):
i = 0
while x % 2 == 0:
i += 1
x /= 2
return -x, i
def main():
n = int(input())
a = list(map(int, input().split()))
a.sort(key=cmp)
for i in range(len(a)):
print(a[i], end=" ")
main()
|
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF 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 VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
l = list(map(int, input().split()))
p = []
for i in range(n):
x, c = l[i], 0
while l[i] % 3 == 0:
l[i] = l[i] // 3
c += 1
p.append([-c, x])
p.sort()
print(" ".join(list(map(lambda o: str(o[1]), p))))
|
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 VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = list(map(int, input().split()))
def md(x, d):
res = 0
dd = 1
while x % (dd * d) == 0:
dd *= d
res += 1
return res
def cmp(a, b):
d3a = md(a, 3)
d2a = md(a, 2)
d3b = md(b, 3)
d2b = md(b, 2)
if d3a == d3b:
return d2a < d2b
return d3a > d3b
for i in range(n):
for j in range(i, n):
if cmp(a[j], a[i]):
a[i], a[j] = a[j], a[i]
print(" ".join(map(str, a)))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
v = lambda p: lambda n: 0 if n % p else 1 + v(p)(n // p)
input()
print(
" ".join(
map(
str, sorted(reversed(sorted(map(int, input().split()), key=v(3))), key=v(2))
)
)
)
|
ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP NUMBER FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
l = list(map(int, input().split()))
d = {}
for t in l:
if t not in d:
d[t] = 1
else:
d[t] += 1
k = [l[0]]
x = l[0]
d[x] -= 1
for t in range(100):
if x * 2 in d and d[x * 2] > 0:
k.append(x * 2)
d[x * 2] -= 1
x *= 2
elif x % 3 == 0 and x // 3 in d and d[x // 3] > 0:
k.append(x // 3)
d[x // 3] -= 1
x = x // 3
x = l[0]
for t in range(100):
if x % 2 == 0 and x // 2 in d and d[x // 2] > 0:
k.insert(0, x // 2)
d[x // 2] -= 1
x = x // 2
elif x * 3 in d and d[x * 3] > 0:
k.insert(0, x * 3)
d[x * 3] -= 1
x = x * 3
print(*k)
|
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 VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def test(l, m, ite, Mite, dest):
if ite == Mite:
return True
dest.append(m)
if m not in l:
dest.pop()
return False
if m % 3 == 0:
if test(l, m // 3, ite + 1, Mite, dest):
return True
else:
return test(l, m * 2, ite + 1, Mite, dest)
else:
return test(l, m * 2, ite + 1, Mite, dest)
n = int(input())
line = [int(e) for e in input().split(" ")]
possible = []
rez = -1
de = []
for e in line:
if not test(line, e, 0, n, de):
de.clear()
else:
break
for e in de:
print(e, end=" ")
print()
|
FUNC_DEF IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
import sys
n = int(sys.stdin.readline())
nums = list(map(int, sys.stdin.readline().split()))
edges = {}
for i in nums:
for j in nums:
if i * 2 == j:
if i in edges:
edges[i].append(j)
else:
edges[i] = [j]
if j * 3 == i:
if i in edges:
edges[i].append(j)
else:
edges[i] = [j]
inin = {}
for i in nums:
inin[i] = 0
for i in edges:
for j in edges[i]:
inin[j] += 1
ans = []
queue = []
for i in nums:
if inin[i] == 0:
queue.append(i)
while len(queue) > 0:
v = queue.pop()
ans.append(v)
if v in edges:
for i in edges[v]:
inin[i] -= 1
if inin[i] == 0:
queue.append(i)
for i in ans:
print(i, end=" ")
|
IMPORT 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 VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR IF BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def fact(x):
r = [0, 0, x]
while x % 2 == 0:
x //= 2
r[0] += 1
while x % 3 == 0:
x //= 3
r[1] -= 1
return r
input()
d = [fact(int(x)) for x in input().split()]
d.sort()
print(*(x[2] for x in d))
|
FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
res = list(map(int, input().strip().split(" ")))
res = sorted(res, reverse=True)
for i in range(len(res)):
k = 0
result = []
result.append(res[i])
while len(result) != len(res):
if result[k] % 3 == 0 and result[k] // 3 in res:
result.append(result[k] // 3)
elif result[k] * 2 in res:
result.append(result[k] * 2)
else:
break
if len(result) == len(res):
break
k += 1
if len(result) == len(res):
break
for i in result:
print(i, end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def comp(d1, d2):
if d1.first > d2.first:
return 1
if d1.first < d2.first:
return 0
return bool(d1.second > d2.second)
n = int(input())
a = [int(x) for x in input().split()]
b = []
for x in a:
tmp = x
c3 = 0
c2 = 0
while x % 3 == 0:
x //= 3
c3 += 1
b.append((-c3, tmp))
b = sorted(b)
for x in b:
print(x[1], end=" ")
|
FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def dfs(v, a, c, t):
c[v] = 1
for nb in adj[v]:
if not c[nb]:
dfs(nb, a, c, t)
t.append(v)
n, a = int(input()), list(map(int, input().split()))
adj = [[] for _ in range(n)]
for i in range(n - 1):
for j in range(i + 1, n):
if a[i] * 2 == a[j] or a[j] * 3 == a[i]:
adj[i].append(j)
elif a[j] * 2 == a[i] or a[i] * 3 == a[j]:
adj[j].append(i)
t = []
c = [0] * n
for i in range(n):
if c[i] == 0:
dfs(i, adj, c, t)
print(" ".join([str(a[i]) for i in t[::-1]]))
|
FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR NUMBER
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = [int(item) for item in input().split()]
def solve(start, occ, d):
if d == n - 1:
return True
u = False
v = False
if start % 3 == 0 and start // 3 in occ and occ[start // 3] != 0:
new_occ = dict(occ)
new_occ[start // 3] = 0
u = solve(start // 3, new_occ, d + 1)
if start * 2 in occ and occ[start * 2] != 0:
new_occ = dict(occ)
new_occ[start * 2] = 0
v = solve(start * 2, new_occ, d + 1)
return u or v
def solve2(start, occ, d):
if d == n - 1:
return True, [start]
u = False, []
v = False, []
if start % 3 == 0 and start // 3 in occ and occ[start // 3] != 0:
new_occ = dict(occ)
new_occ[start // 3] = 0
u = solve2(start // 3, new_occ, d + 1)
if start * 2 in occ and occ[start * 2] != 0:
new_occ = dict(occ)
new_occ[start * 2] = 0
v = solve2(start * 2, new_occ, d + 1)
if u[0]:
return True, [start] + u[1]
if v[0]:
return True, [start] + v[1]
return False, []
occ = {key: (0) for key in a}
for i in range(n):
occ[a[i]] += 1
for i in range(n):
new_occ = dict(occ)
new_occ[a[i]] -= 1
if solve(a[i], new_occ, 0):
new_occ = dict(occ)
new_occ[a[i]] -= 1
print(" ".join(str(item) for item in solve2(a[i], new_occ, 0)[1]))
break
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN NUMBER LIST VAR ASSIGN VAR NUMBER LIST ASSIGN VAR NUMBER LIST IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER BIN_OP LIST VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER BIN_OP LIST VAR VAR NUMBER RETURN NUMBER LIST ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
from sys import stdin, stdout
def dfs(n):
global ans
ans += [n]
if n in twice:
dfs(twice[n])
elif n in div3:
dfs(div3[n])
for _ in range(1):
n = int(stdin.readline())
ans = []
a = list(map(int, stdin.readline().split()))
twice = {}
div3 = {}
s = set(a)
ind = {}
outd = {}
for v in a:
if 2 * v in s:
twice[v] = 2 * v
ind[2 * v] = ind.get(2 * v, 0) + 1
outd[v] = outd.get(v, 0) + 1
if v % 3 == 0 and v // 3 in s:
div3[v] = v // 3
ind[v // 3] = ind.get(v // 3, 0) + 1
outd[v] = outd.get(v, 0) + 1
for v in a:
if v not in ind:
src = v
break
dfs(src)
print(*ans)
|
FUNC_DEF VAR LIST VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR IF BIN_OP NUMBER VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
lst = list(map(int, input().split()))
def conv(x):
ans = 0
while x % 2 == 0:
ans += 1
x = x // 2
return -x, ans
lst.sort(key=conv)
print(*lst)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = [int(x) for x in input().split()]
a.sort()
l1 = [-1] * n
l2 = [-1] * n
for i in range(0, n):
t = a[i]
t2 = t * 2
j = i + 1
f = False
while j < n:
if a[j] > t2:
break
if a[j] == t2 and l2[j] == -1:
f = True
break
j += 1
if f:
l1[i] = j
l2[j] = i
if t % 3 == 0:
t3 = t // 3
j = i - 1
f = False
while j > -1:
if a[j] < t3:
break
if a[j] == t3 and l2[j] == -1:
f = True
break
j -= 1
if f:
l1[i] = j
l2[j] = i
p = l2.index(-1)
while l1[p] != -1:
print(a[p], end=" ")
p = l1[p]
print(a[p])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def pow3(a):
if a % 3 != 0:
return 0
return 1 + pow3(a // 3)
n = int(input())
a = list(map(int, input().split()))
b = []
for i in a:
b.append((-pow3(i), i))
b.sort()
for i in b:
print(i[1], end=" ")
|
FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR BIN_OP 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 LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
f = int(input())
b = input()
c = b.split()
d = []
e = 0
for i in range(f):
d.append(int(c[i]))
for i in range(f):
check = 0
for j in range(f):
if d[j] == 3 * d[i]:
check = 1
if d[i] % 2 == 0 and d[i] == 2 * d[j]:
check = 1
if check == 0:
e = d[i]
print(e, end=" ")
for i in range(f - 1):
for j in range(f):
if 3 * d[j] == e:
e = d[j]
break
elif 2 * e == d[j]:
e = d[j]
break
print(e, end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
nums = list(map(int, input().split()))
nums.sort()
ans = []
for i in range(n):
aux = nums[:]
a = [nums[i]]
j = 0
while a[j] // 3 in aux or a[j] * 2 in aux:
b = a[j] // 3
c = a[j] * 2
if c in aux:
a.append(c)
aux.remove(c)
elif a[j] == 4:
break
else:
a.append(b)
aux.remove(b)
j += 1
ans.append(a)
ans.sort(key=len)
for item in ans[-1]:
print(item, end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
import sys
n = int(input())
lis = list(map(int, input().split()))
for j in range(n):
ans = []
curr = lis[j]
cnt = 0
while cnt < n:
ans.append(curr)
cnt += 1
if curr * 2 in lis:
curr *= 2
elif curr // 3 in lis and curr % 3 == 0:
curr = curr // 3
else:
break
if cnt == n:
ans = [str(p) for p in ans]
print(" ".join(ans))
sys.exit(0)
print(-1)
|
IMPORT 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 LIST ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def F(num, a_res, a):
a_new = a[:]
a_new.remove(num)
a_res.append(num)
if num % 3 == 0:
if num // 3 in a_new:
a_res = F(num // 3, a_res, a_new)
if num * 2 in a_new:
a_res = F(num * 2, a_res, a_new)
return a_res
n = int(input())
a = [int(i) for i in input().split()]
for i in range(0, n):
num = a[i]
a_res = []
a_res = F(num, a_res, a)
if len(a_res) == n:
break
result = ""
for i in a_res:
result = result + " " + str(i)
print(result)
|
FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
numbers = list(map(int, input().split(" ")))
divide_by_three = {}
for i in numbers:
j = 0
while i % 3**j == 0:
j += 1
divide_by_three[i] = j
num = [(-v, k) for k, v in divide_by_three.items()]
num = sorted(num)
print(*[i[1] for i in num])
|
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 DICT FOR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def exp_3(a: int) -> int:
ans = 0
while a % 3 == 0:
a //= 3
ans += 1
return ans
def exp_2(a: int) -> int:
ans = 0
while a % 2 == 0:
a //= 2
ans += 1
return ans
def comp(a: int, b: int) -> bool:
if exp_3(a) > exp_3(b):
return -1
elif exp_3(a) < exp_3(b):
return 1
elif exp_2(a) < exp_2(b):
return -1
else:
return 1
def cmp_to_key(mycmp):
class K:
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return mycmp(self.obj, other.obj) < 0
def __gt__(self, other):
return mycmp(self.obj, other.obj) > 0
def __eq__(self, other):
return mycmp(self.obj, other.obj) == 0
def __le__(self, other):
return mycmp(self.obj, other.obj) <= 0
def __ge__(self, other):
return mycmp(self.obj, other.obj) >= 0
def __ne__(self, other):
return mycmp(self.obj, other.obj) != 0
return K
def solve(n: int, A: list):
print(" ".join(list(map(str, sorted(A, key=cmp_to_key(comp))))))
n = int(input())
A = list(map(int, input().split()))
solve(n, A)
|
FUNC_DEF VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER VAR FUNC_DEF CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
inputlist = list(map(int, input().split()))
outputlist = [inputlist.pop(0)]
while len(inputlist) > 0:
for i in range(len(inputlist)):
if outputlist[0] * 3 == inputlist[i]:
outputlist = [inputlist.pop(i)] + outputlist
break
elif inputlist[i] * 3 == outputlist[-1]:
outputlist = outputlist + [inputlist.pop(i)]
break
elif outputlist[-1] * 2 == inputlist[i]:
outputlist = outputlist + [inputlist.pop(i)]
break
elif inputlist[i] * 2 == outputlist[0]:
outputlist = [inputlist.pop(i)] + outputlist
break
print(" ".join(list(map(str, outputlist))))
|
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 FUNC_CALL VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR LIST FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR LIST FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
lst = [int(x) for x in input().split()]
n = len(lst)
def sol(lst):
res = []
for i in range(n):
tempLst = lst.copy()
res += [lst[i]]
tempLst.remove(lst[i])
for j in range(1, n):
for k in range(len(tempLst)):
if tempLst[k] == res[len(res) - 1] * 2:
res += [tempLst[k]]
tempLst.remove(tempLst[k])
break
elif (
res[len(res) - 1] / 3 % 1 == 0
and tempLst[k] == res[len(res) - 1] // 3
):
res += [tempLst[k]]
tempLst.remove(tempLst[k])
break
if len(res) == n:
for i in res:
print(str(i) + " ", end="")
return
else:
res = []
print(str(111))
sol(lst)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING STRING RETURN ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
def calc(x0):
v2, v3 = 0, 0
x = int(x0)
while x % 3 == 0:
v3 += 1
x = x // 3
while x % 2 == 0:
v2 += 1
x = x // 2
return tuple([v2, -v3, x0])
a = list(map(calc, input().split()))
a = sorted(a)
print(" ".join(map(lambda x: str(x[2]), a)))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def dfs(d, x):
stack = [x]
res = 1
while stack:
v = stack.pop()
a = 0
if v % 3 == 0:
e = v // 3
if d.get(e) != None:
if d[e] > 0:
d[e] -= 1
stack.append(e)
res += 1
a = 1
r = v * 2
if d.get(r) != None:
if d[r] > 0:
d[r] -= 1
stack.append(r)
if a == 0:
res += 1
return res
n = int(input())
mas = list(map(int, input().split()))
di = {}
for i, x in enumerate(mas):
if di.get(x) == None:
di[x] = 0
di[x] += 1
result = {}
for i, x in enumerate(mas):
d = dict(di)
res = dfs(d, x)
result[res] = x
for i in range(n, 0, -1):
print(result[i], end=" ")
|
FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NONE IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NONE IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER 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 ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def info(x):
copy_x = x
cnt3 = 0
cnt2 = 0
while x % 3 == 0:
x = x // 3
cnt3 += 1
while x % 2 == 0:
x = x // 2
cnt2 -= 1
return cnt3, cnt2, copy_x
n = int(input())
a = [int(i) for i in input().split()]
new_a = []
for i in a:
new_a.append(info(i))
new_a.sort()
for i in range(len(new_a) - 1, -1, -1):
print(new_a[i][2], end=" ")
|
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def chooseN(k, arr):
arr.append(k)
global nexti, during
if during[k] == 0:
return False
during[k] -= 1
if len(nexti[k]) == 0:
if max([during[x] for x in during.keys()]) != 0:
chooseP(arr[0], list(reversed(arr.copy())))
else:
print(" ".join([str(x) for x in arr]))
during[k] += 1
return True
for newk in nexti[k]:
if chooseN(newk, arr.copy()):
during[k] += 1
return True
during[k] += 1
return False
def chooseP(k, arr):
arr.append(k)
global prev, during
if during[k] == 0:
return False
during[k] -= 1
if not prev[k]:
if max([during[x] for x in during.keys()]) != 0:
during[k] += 1
return False
else:
print(" ".join([str(x) for x in reversed(arr)]))
during[k] += 1
return True
for p in prev[k]:
if chooseP(p, arr.copy()):
during[k] += 1
return True
during[k] += 1
return False
def first():
global prev
for p in prev:
if len(prev[p]) == 0:
return p
return -1
def any():
global ls, __c
__c += 1
print(ls, __c)
return ls[__c]
n, ls, nexti, total, prev, __c = (
int(input()),
list(map(int, input().split(" "))),
{},
{},
{},
-1,
)
for i in range(n):
k = ls[i]
if k in total:
total[k] += 1
else:
total[k] = 1
nexti[k] = []
prev[k] = []
if k % 3 == 0 and k // 3 in ls:
nexti[k].append(k // 3)
if k * 2 in ls:
nexti[k].append(k * 2)
if k * 3 in ls:
prev[k].append(k * 3)
if k % 2 == 0 and k // 2 in ls:
prev[k].append(k // 2)
during = total.copy()
start = first()
if start == -1:
start = any()
while not chooseN(start, []):
start = any()
|
FUNC_DEF EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER FOR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER FOR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER RETURN NUMBER FUNC_DEF FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN VAR RETURN NUMBER FUNC_DEF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING DICT DICT DICT NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR LIST ASSIGN VAR VAR LIST IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR LIST ASSIGN VAR FUNC_CALL VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def canTake(nbr, lst, dict):
return dict[nbr] != lst.count(nbr)
def exists(nbr, dict):
return nbr in dict
def dfs(nbr, lst, dict, n):
if len(lst) == n:
print(*lst)
exit(0)
possibilities = []
if exists(nbr * 2, dict) and canTake(nbr * 2, lst, dict):
possibilities.append(nbr * 2)
if nbr % 3 == 0 and exists(nbr // 3, dict) and canTake(nbr // 3, lst, dict):
possibilities.append(nbr // 3)
if len(possibilities) == 0:
return []
for item in possibilities:
lst.append(item)
dfs(item, lst, dict, n)
def main():
n = int(input())
arr = map(int, input().split())
dict = {}
for item in arr:
if exists(item, dict):
dict[item] += 1
else:
dict[item] = 1
for item in dict:
dfs(item, [item], dict, n)
main()
|
FUNC_DEF RETURN VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n, s, v = int(input()), set(map(int, input().split())), []
for x in s:
if (x % 2 or x // 2 not in s) and x * 3 not in s:
v.append(x)
for i in range(n - 1):
if v[-1] % 3 == 0 and v[-1] // 3 in s:
v.append(v[-1] // 3)
elif v[-1] * 2 in s:
v.append(v[-1] * 2)
print(" ".join(map(str, v)))
|
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def func(a):
b = a
c, d = 0, 0
while a % 3 == 0:
a = a // 3
c = c + 1
while b % 2 == 0:
b = b // 2
d = d + 1
if d == 0:
return c + 2
return c + 1 / d
n = int(input())
a = list(map(int, input().strip().split()))
b = []
for i in range(n):
b.append([func(a[i]), a[i]])
b.sort(reverse=True)
c = []
for i in range(n):
c.append(b[i][1])
print(*c)
|
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def t(v: int):
count = 0
while v % 3 == 0:
count += 1
v //= 3
return count
n = int(input())
a = list(map(int, input().split()))
ans = []
for i in a:
ans.append((-t(i), i))
d = dict()
for i in ans:
if i[0] in d:
d[i[0]].append(i[1])
else:
d[i[0]] = [i[1]]
for i in sorted(list(d.keys())):
print(*sorted(d[i]), end=" ")
|
FUNC_DEF VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER 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 ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER LIST VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
from sys import stdin, stdout
cin = stdin.readline
cout = stdout.write
n = int(cin())
a = list(map(int, cin().split()))
l = [0] * n
for i in a:
l[0] = i
for j in range(n - 1):
if l[j] % 3 == 0 and l[j] // 3 in a:
l[j + 1] = l[j] // 3
elif l[j] * 2 in a:
l[j + 1] = l[j] * 2
else:
break
else:
for j in l:
cout(str(j) + " ")
|
ASSIGN VAR VAR 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 BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
l = list(map(int, input().split()))
l.sort()
l1 = []
l2 = []
cur = l[0]
l1.append(cur)
while True:
if cur % 3 == 0 and cur // 3 in l:
cur //= 3
l1.append(cur)
elif cur * 2 in l:
cur *= 2
l1.append(cur)
else:
break
cur = l[0]
while True:
if cur % 2 == 0 and cur // 2 in l:
cur //= 2
l2.append(cur)
elif cur * 3 in l:
cur *= 3
l2.append(cur)
else:
break
for x in reversed(l2):
print(x, end=" ")
for x in l1:
print(x, end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def func(n, m):
m = m.split()
m = [int(m[i]) for i in range(len(m))]
result = [m[0]]
count = 0
countall = 0
m.sort(key=lambda p: (-power(p)[1], power(p)[0]))
return " ".join(str(m[i]) for i in range(len(m)))
def power(n):
double = 0
triple = 0
while n % 2 == 0 or n % 3 == 0:
if n % 2 == 0:
n = n // 2
double += 1
if n % 3 == 0:
n = n // 3
triple += 1
return [double, triple]
t = [1] + [(2**i) for i in range(99, 0, -1)]
print(func(int(input().strip()), input().strip()))
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN LIST VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
l = [int(i) for i in input().split()]
k = [l[0]]
z = w = l[0]
l.remove(w)
while True:
if w * 2 in l:
w = w * 2
k = k + [int(w)]
l.remove(w)
elif w % 3 == 0 and w // 3 in l:
w = w // 3
k = k + [w]
l.remove(w)
else:
break
w = z
while True:
if w % 2 == 0 and w // 2 in l:
w = w // 2
k = [w] + k
l.remove(w)
elif w * 3 in l:
w = w * 3
k = [w] + k
l.remove(w)
else:
break
print(*k)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR LIST FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def traverse(num, nums, position):
copy = nums[:]
position = position[:]
if num not in copy:
return []
else:
copy.remove(num)
position.append(num)
if len(copy) == 0:
return position
elif num % 3 == 0:
a = traverse(num // 3, copy, position)
b = traverse(num * 2, copy, position)
if len(a) > 0:
return a
else:
return b
else:
return traverse(num * 2, copy, position)
numbers = int(input())
nums = [int(x) for x in input().split(" ")]
for i in nums:
total = traverse(i, nums, [])
if len(total) > 0:
print(" ".join([str(x) for x in total]))
break
|
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR RETURN LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR LIST IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
from sys import stdin, stdout
def rearrage_polycarp_math_and_sorting_solution(n: int, sequence):
def exp(b: int, a: int) -> int:
k = 0
while a % b == 0:
k += 1
a //= b
return k
power_of_3 = [(-exp(3, num), num) for num in sequence]
power_of_3.sort()
return [num[1] for num in power_of_3]
def rearrage_polycarp(n: int, sequence):
polys_seq = set(sequence)
ordered_seq = [
num
for num in sequence
if not (num % 2 == 0 and num // 2 in polys_seq or 3 * num in polys_seq)
]
n -= 1
i = 0
while n:
if 2 * ordered_seq[i] in polys_seq:
ordered_seq.append(2 * ordered_seq[i])
else:
ordered_seq.append(ordered_seq[i] // 3)
n -= 1
i += 1
return ordered_seq
n = int(stdin.readline().rstrip())
sequence = [int(x) for x in stdin.readline().rstrip().split(" ")]
stdout.write(
" ".join(
str(num) for num in rearrage_polycarp_math_and_sorting_solution(n, sequence)
)
+ "\n"
)
|
FUNC_DEF VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR NUMBER VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = list(map(int, input().split()))
b = {}
for i in range(n):
x = a[i]
cell = [0, 0]
if x % 3 == 0:
if x // 3 in a:
cell[0] = 1
if 2 * x in a:
cell[1] = 1
if not cell[0] and not cell[1]:
last = x
b[x] = cell
seq = [last]
while n > 1:
x = seq[-1]
n -= 1
if x & 1:
seq.append(x * 3)
else:
try:
k = b[x // 2]
seq.append(x // 2)
except:
seq.append(x * 3)
print(*seq[::-1])
|
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 VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER IF BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR WHILE VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = [int(i) for i in input().split()]
after = {}
f = -1
for i in range(n):
if a[i] % 2 == 0 and a[i] // 2 in a:
after[a[i] // 2] = a[i]
elif a[i] * 3 in a:
after[a[i] * 3] = a[i]
else:
f = a[i]
for i in range(n):
print(f, end=" ")
if i != n - 1:
f = after[f]
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
import sys
try:
sys.stdin = open("input.txt", "r")
sys.stdout = open("output.txt", "w")
except Exception as es:
pass
n = int(input())
lst = list(map(int, input().split()))
ans = []
for i in range(n):
for a in lst:
if a * 2 in lst or a % 3 == 0 and a // 3 in lst:
continue
else:
ans.append(a)
lst.remove(a)
print(" ".join([str(x) for x in ans[::-1]]))
|
IMPORT ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING 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 LIST FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = map(int, input().split())
b = []
for x in a:
y = x
cnt = 0
while y % 3 == 0:
y //= 3
cnt += 1
b.append([-cnt, x])
b = sorted(b)
for x in b:
print(x[1], end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def possible(arr, x):
nums = {}
for i in arr:
if i not in nums.keys():
nums[i] = 0
nums[i] += 1
ans = [x]
while True:
if x % 3 == 0 and x // 3 in nums.keys():
x = x // 3
elif 2 * x in nums.keys():
x = 2 * x
else:
return False
ans.append(x)
nums[x] -= 1
if nums[x] == 0:
del nums[x]
if len(ans) == len(arr):
break
for i in ans:
print(i, end=" ")
return True
def main():
n = int(input())
arr = list(map(int, input().split()))
for i in arr:
if possible(arr, i):
return
main()
|
FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP NUMBER VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def vert(n):
if n * 2 in a:
yield n * 2
if n % 3 == 0 and n // 3 in a:
yield n // 3
def dfs(i, arr, b):
if len(arr) == n:
print(" ".join(map(str, arr)))
return
for j in vert(i):
c = b.copy()
c.remove(j)
dfs(j, arr + [j], c)
n = int(input())
a = set([int(i) for i in input().split()])
for i in a:
l = []
b = a.copy()
b.remove(i)
dfs(i, l + [i], b)
|
FUNC_DEF IF BIN_OP VAR NUMBER VAR EXPR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR BIN_OP VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
from sys import stdin, stdout
def main():
n = int(stdin.readline())
a = [int(i) for i in stdin.readline().split()]
d = dict()
for v in a:
v3 = 0
vc = v
while vc % 3 == 0:
vc = vc // 3
v3 += 1
temp = d.get(v3, [])
temp.append(v)
d[v3] = temp
k = list(d.keys())
k.sort()
k.reverse()
out = []
for i in k:
temp = d[i]
temp.sort()
out += temp
stdout.write(" ".join([str(i) for i in out]) + "\n")
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = [int(i) for i in input().split()]
p = []
for i in range(n):
for j in range(i + 1, n):
if a[i] == a[j] * 2:
p.append([a[i], a[j]])
if a[j] == a[i] * 2:
p.append([a[j], a[i]])
if a[i] == a[j] * 3:
p.append([a[j], a[i]])
if a[j] == a[i] * 3:
p.append([a[i], a[j]])
d = dict()
d[p[0][0]] = 0
for j in range(n - 1):
for i in range(n - 1):
if d.get(p[i][0]) != None:
d[p[i][1]] = d[p[i][0]] - 1
if d.get(p[i][1]) != None:
d[p[i][0]] = d[p[i][1]] + 1
ans = []
ans_p = []
for i, j in d.items():
ans.append(i)
ans_p.append(j)
prov = []
for i in range(len(ans_p)):
prov.append(ans_p[i])
prov.sort()
while ans_p != prov:
for i in range(n - 1):
if ans_p[i] > ans_p[i + 1]:
ans_p[i], ans_p[i + 1] = ans_p[i + 1], ans_p[i]
ans[i], ans[i + 1] = ans[i + 1], ans[i]
for i in range(len(ans)):
print(ans[i], end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NONE ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NONE ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR WHILE VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
lst = [int(x) for x in input().split()]
l = -1
for x in range(n):
if lst[x] % 2 == 0:
if not lst[x] // 2 in lst and not lst[x] * 3 in lst:
l = x
break
elif not lst[x] * 3 in lst:
l = x
break
if l != -1:
x = lst[l]
else:
x = min(lst)
array = []
while n > 0:
if x % 3 == 0 and x // 3 in lst:
array.append(x)
x //= 3
else:
array.append(x)
x *= 2
n -= 1
print(*array)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
arr2 = []
arr = [int(i) for i in input().split()]
for s in arr:
k = s
n3 = 0
n2 = 0
while k % 3 == 0:
k = k % 3 + k // 3
n3 += 1
while k % 2 == 0:
k = k // 2 + k % 2
n2 += 1
arr2.append((n3, n2, s))
arr2 = sorted(arr2, key=lambda x: -x[0])
b = arr2[0][0]
while b >= 0:
a = sorted(list(filter(lambda x: x[0] == b, arr2)), key=lambda x: x[1])
for j in a:
print(j[2], end=" ")
b -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING VAR NUMBER
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
L = list(map(int, input().split()))
L.sort()
min = L[0]
L1 = [L[0]]
while True:
if min * 3 in L and min * 3 not in L1:
min = min * 3
L1 = [min] + L1
elif min / 2 in L and min / 2 not in L1:
min = min // 2
L1 = [min] + L1
else:
break
min = L[0]
while True:
if min / 3 in L and min / 3 not in L1:
min = min // 3
L1.append(min)
elif min * 2 in L and min * 2 not in L1:
min = min * 2
L1.append(min)
else:
break
for i in L1:
print(i, end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER WHILE NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR NUMBER WHILE NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def pow3(x):
ans = 0
while x % 3 == 0:
x = x // 3
ans += 1
return ans
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
a[i] = [-1 * pow3(a[i]), a[i]]
a.sort()
for i in range(n):
a[i] = a[i][1]
print(*a)
|
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
def dfs(v):
used[v] = 1
now.append(v)
for i in range(len(graph[v])):
if not used[graph[v][i]]:
dfs(graph[v][i])
n = int(input())
num = list(map(int, input().split()))
graph = [[] for i in range(n)]
for i in range(n):
for j in range(n):
if i != j:
if num[j] * 3 == num[i]:
graph[i].append(j)
elif num[i] * 2 == num[j]:
graph[i].append(j)
for i in range(n):
now = []
used = [0] * n
dfs(i)
if len(now) == n:
ans = []
for i in range(n):
ans.append(num[now[i]])
print(*ans)
|
FUNC_DEF ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR 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 LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
-----Input-----
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
-----Output-----
Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
-----Examples-----
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
-----Note-----
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
|
n = int(input())
a = list(map(int, input().split()))
def calc_deg3(x):
d = 0
while x % 3 == 0:
x //= 3
d += 1
return d
m = {}
for ai in a:
d = calc_deg3(ai)
if d not in m:
m[d] = []
m[d].append(ai)
answer = []
for d in sorted(m.keys(), reverse=True):
answer.extend(sorted(m[d]))
print(" ".join(map(str, answer)))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.