description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def valid(self, k, n):
return k * (k + 1) * (2 * k + 1) // 6 <= n
def killinSpree(self, n):
lower = 1
upper = n
while lower < upper - 1:
candidate = (upper + lower) // 2
if self.valid(candidate, n):
lower = candidate
else:
upper = candidate
return lower
|
CLASS_DEF FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def killinSpree(self, n):
cnt = -1
start = 0
end = n
while start <= end:
mid = (start + end) // 2
midSquareSum = int(mid * (mid + 1) * (2 * mid + 1) / 6)
if midSquareSum <= n:
cnt = mid
start = mid + 1
else:
end = mid - 1
return cnt
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def killinSpree(self, n):
start = 0
end = 100000.0
mid = start + (end - start) // 2
ans = 0
while start <= end:
condition = mid * (mid + 1) * (2 * mid + 1) // 6
if n >= condition:
ans = mid
start = mid + 1
else:
end = mid - 1
mid = start + (end - start) // 2
return int(ans)
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def killinSpree(self, n):
l = 1
h = n
while l <= h:
m = l + h >> 1
if m * (m + 1) * (2 * m + 1) // 6 == n:
return m
elif m * (m + 1) * (2 * m + 1) // 6 > n:
h = m - 1
else:
l = m + 1
return h
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR RETURN VAR IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def binarysearch(self, l, r, n):
if l <= r:
mid = (l + r) // 2
s = mid * (mid + 1) * (2 * mid + 1) // 6
if s <= n:
return self.binarysearch(mid + 1, r, n)
else:
return self.binarysearch(l, mid - 1, n)
return r
def killinSpree(self, n):
return self.binarysearch(0, 1000000, n)
|
CLASS_DEF FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER NUMBER VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def killinSpree(self, n):
start = 1
end = 10**6
res = 0
while start <= end:
mid = start + (end - start) // 2
value = mid * (mid + 1) * (2 * mid + 1) // 6
if value == n:
res = mid
break
elif value < n:
res = mid
start = mid + 1
else:
end = mid - 1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
def getSum(n):
return n * (n + 1) * (2 * n + 1) / 6
class Solution:
def killinSpree(self, n):
bot = 1
top = n
result = 0
while bot <= top:
mid = (bot + top) // 2
s = getSum(mid)
if s <= n:
result = mid
bot = mid + 1
else:
top = mid - 1
return result
|
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def killinSpree(self, n):
def squareSum(x):
return x * (x + 1) * (2 * x + 1) // 6
low = 1
high = 1000000.0
result = -1
while low <= high:
mid = int((low + high) // 2)
if squareSum(mid) > n:
high = mid - 1
elif squareSum(mid) < n:
result = mid
low = mid + 1
else:
return mid
return result
|
CLASS_DEF FUNC_DEF FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR RETURN VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def killinSpree(self, n):
low = 1
high = n + 1
while low < high:
mid = (low + high + 1) // 2
if n >= mid * (mid + 1) * (2 * mid + 1) // 6:
low = mid
else:
high = mid - 1
return low
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def killinSpree(self, n):
l, h = 0, n
while l <= h:
mid = (l + h) // 2
if 2 * mid * mid * mid + 3 * mid * mid + mid <= 6 * n:
l = mid + 1
else:
h = mid - 1
return l - 1
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def Formula(self, n):
return n * (n + 1) * (2 * n + 1) // 6
def killinSpree(self, n):
l = 1
h = 10000000.0
ans = 0
mid = l + (h - l) // 2
if n == 0:
return 0
while l <= h:
mid = l + (h - l) // 2
if int(self.Formula(mid)) <= n:
ans = mid
l = mid + 1
else:
h = mid - 1
return int(ans)
|
CLASS_DEF FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def killinSpree(self, n):
s = 0
ans = 0
i = 1
while n > 0:
s = i * (i + 1) * (2 * i + 1) // 6
if n > s:
i = 2 * i
elif n == s:
ans = i
break
else:
low = i // 2
high = i
while low <= high:
mid = (low + high) // 2
s = mid * (mid + 1) * (2 * mid + 1) // 6
if s > n:
high = mid - 1
elif s == n:
ans = mid
break
else:
nex = (mid + 1) * (mid + 1 + 1) * (2 * (mid + 1) + 1) // 6
if nex > n:
ans = mid
break
low = mid + 1
break
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def killinSpree(self, n):
if n == 1:
return 1
def findSum(k):
return k * (k + 1) * (2 * k + 1) // 6
res = 0
start = 1
end = n
while start < end:
mid = (start + end) // 2
sm = findSum(mid)
if sm == mid:
return mid
if sm > n:
end = mid
else:
res = mid
start = mid + 1
return res
|
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def killinSpree(self, n):
def vaild(target, n):
temp = target * (target + 1) * (2 * target + 1) // 6
return n >= temp
start = 1
end = n
ans = -1
while start <= end:
mid = (start + end) // 2
if vaild(mid, n):
ans = mid
start = mid + 1
else:
end = mid - 1
return ans
|
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def killinSpree(self, n):
hi = int((6 * n) ** (1 / 3))
lo = 1
while lo <= hi:
mid = (lo + hi) // 2
res = mid * (mid + 1) * (2 * mid + 1)
if res <= 6 * n and (mid + 1) * (mid + 2) * (2 * mid + 3) > 6 * n:
return mid
elif res > 6 * n:
hi = mid - 1
else:
lo = mid + 1
return 0
if __name__ == "__main__":
ob = Solution()
t = int(input())
for _ in range(t):
N = int(input())
ans = ob.killinSpree(N)
print(ans)
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR RETURN VAR IF VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def killinSpree(self, n):
l = 0
h = n
while l <= h:
m = (l + h) // 2
if m * (m + 1) * (2 * m + 1) // 6 > n:
h = m - 1
else:
l = m + 1
for i in range(m - 2, m + 2):
if i * (i + 1) * (2 * i + 1) // 6 > n:
return i - 1
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR RETURN BIN_OP VAR NUMBER
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def killinSpree(self, n):
def _solve(x):
return x * (x + 1) * (2 * x + 1) // 6
L, R = 0, 20000
while L < R:
m = (L + R) // 2
if _solve(m) <= n:
L = m + 1
else:
R = m
return L - 1
|
CLASS_DEF FUNC_DEF FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def killinSpree(self, n):
i = 1
j = n
while i < j:
mid = i + j + 1 >> 1
ans = (mid + 1) * (mid * 2 + 1) * mid // 6
if ans > n:
j = mid - 1
else:
i = mid
return i
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def killinSpree(self, n):
def add(x):
return x * (x + 1) * (2 * x + 1) // 6
if n == 0:
return 0
low = 1
high = 10000000.0
ans = 0
while low <= high:
mid = low + (high - low) // 2
sum = add(mid)
if sum <= n:
ans = mid
low = mid + 1
else:
high = mid - 1
return int(ans)
|
CLASS_DEF FUNC_DEF FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
MAX = int(1000000000000.0)
dp = []
def compute():
i = 1
while i * i <= MAX:
dp.append(i * i)
i += 1
def bs(key):
l = 0
r = len(dp) - 1
ans = 0
while l <= r:
mid = l + (r - l) >> 1
if dp[mid] == key:
return mid
if dp[mid] < key:
l = mid + 1
else:
r = mid - 1
return ans
class Solution:
def killinSpree(self, n):
def sumt(h):
return h * (h + 1) * (2 * h + 1) / 6
h = 1
AC = 0
sta = 0
endd = 144225
while sta <= endd:
midd = (sta + endd) // 2
if sumt(midd) <= n:
AC = max(AC, midd)
sta = midd + 1
else:
endd = midd - 1
return AC
|
ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def ss(self, n):
return n * (n + 1) * (2 * n + 1) // 6
def killinSpree(self, n):
l, r = 1, n
while l != r:
mid = (l + r) // 2
if mid == l:
return l if self.ss(r) > n else r
if self.ss(mid) > n:
r = mid
elif self.ss(mid) < n:
l = mid
else:
return mid
return l
|
CLASS_DEF FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR RETURN VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def sum_n2(self, n):
return n * (n + 1) * (2 * n + 1) / 6
def killinSpree(self, P):
i = 1
count = 0
l = 1
r = P
while l <= r:
i = (l + r) // 2
sum = self.sum_n2(i)
if P == sum:
return i
if P > sum:
l = i + 1
if P < sum:
r = i - 1
return l - 1
|
CLASS_DEF FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def killinSpree(self, n):
low = 0
up = n
ans = 0
while low <= up:
mid = (low + up) // 2
power = mid * (mid + 1) * (2 * mid + 1)
if power <= n * 6:
low = mid + 1
ans = max(ans, mid)
else:
up = mid - 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
There are Infinite People Standing in a row, indexed from 1.
A person having index 'i' has strength of (i*i).
You have Strength 'P'. You need to tell what is the maximum number of People You can Kill With your Strength P.
You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
Example 1:
Input:
N = 14
Output: 3
Explanation:
The strengths of people is 1, 4, 9, 16, ....
and so on. WE can kill the first 3 person ,
after which our Power becomes 0 and we cant
kill anyone else. So answer is 3
Example 2:
Input:
N = 10
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function killinSpree() which takes the integer N as input parameters and returns the maximum Number of People You can kill.
Expected Time Complexity: O(log(n))
Expected Auxiliary Space: O(1)
Constraints:
1 β€ T β€ 10^{3}
1 β€ N β€ 10^{12}
|
class Solution:
def killinSpree(self, n):
low, high = 1, 2**32 - 1
while low <= high:
mid = (high + low) // 2
temp = mid * (mid + 1) * (2 * mid + 1) // 6
if temp > n:
high = mid - 1
elif temp == n:
return mid
else:
low = mid + 1
return low - 1
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN 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$.
|
n = int(input())
a = list(map(int, input().split()))
sa = set(a)
def dfs(node, li):
if len(li) == n:
print(*li)
exit(0)
if node * 2 in sa:
dfs(node * 2, li + [node * 2])
if node % 3 == 0 and node // 3 in sa:
dfs(node // 3, li + [node // 3])
for i in range(n):
dfs(a[i], [a[i]])
|
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 VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR 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$.
|
def ans():
N = int(input())
A = list(map(int, input().split()))
Ans = []
for i in range(N):
a = 0
b = 0
c = A[i]
while A[i] > 0 and c % 3 == 0:
c = c // 3
a += 1
while A[i] > 0 and c % 2 == 0:
c = c // 2
b += 1
Ans += [[a, b]]
ss = A[i] // (3**a * 2**b)
Ans = sorted(Ans)
f = []
Ans = Ans[::-1]
anss = []
for i in range(1, len(Ans)):
if Ans[i][0] == Ans[i - 1][0]:
f += [Ans[i - 1][::-1]]
else:
f += [Ans[i - 1][::-1]]
f = sorted(f)
for k in range(len(f)):
f[k] = f[k][::-1]
anss += f
f = []
if Ans[len(Ans) - 1] != Ans[len(Ans) - 2]:
f += [Ans[i][::-1]]
f = sorted(f)
for k in range(len(f)):
f[k] = f[k][::-1]
anss += f
for k in range(len(anss)):
if k != len(anss) - 1:
print(ss * 3 ** anss[k][0] * 2 ** anss[k][1], end=" ")
else:
print(ss * 3 ** anss[k][0] * 2 ** anss[k][1])
ans()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR LIST LIST VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR LIST VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR LIST IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR LIST VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER 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 = input().split()
ls = [[] for i in range(50)]
for i in range(n):
a[i] = int(a[i])
x = a[i]
count = 0
while x % 3 == 0:
x = x // 3
count += 1
ls[count].append(a[i])
for i in range(50):
ls[50 - i - 1].sort()
for j in range(len(ls[50 - i - 1])):
print(ls[50 - i - 1][j], end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER 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())
li = list(map(int, input().split()))
g = {}
def dfs(e, v, order):
if e not in v:
v[e] = 1
if e % 3 == 0:
if e // 3 in g:
order.append(e // 3)
dfs(e // 3, v, order)
if e * 2 in g:
order.append(e * 2)
dfs(e * 2, v, order)
for i in li:
g[i] = 0
for i in li:
vis = {}
Aorder = []
Aorder.append(i)
dfs(i, vis, Aorder)
if len(Aorder) == n:
print(*Aorder)
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 DICT FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF 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$.
|
def to(a):
s = 0
while a % 2 == 0 and a != 0:
s += 1
a = a // 2
return s
def tr(a):
s = 0
while a % 3 == 0 and a != 0:
s += 1
a = a // 3
return s
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
for j in range(i + 1, n):
if to(a[i]) > to(a[j]):
a[i], a[j] = a[j], a[i]
if tr(a[i]) < tr(a[j]):
a[i], a[j] = a[j], a[i]
print(*a)
|
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR 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())
def k(x):
twos = 0
while x % 2 == 0:
x //= 2
twos += 1
threes = 0
while x % 3 == 0:
x //= 3
threes += 1
return twos, -threes
A = sorted(map(int, input().split()), key=k)
print(" ".join(map(str, A)))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF 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 RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL 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$.
|
from sys import stdin, stdout
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(n, sequence)) + "\n")
|
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$.
|
def found_sequence(possible_sequence, lookup):
if len(possible_sequence) == n:
print(" ".join(list(map(str, possible_sequence))))
return True
else:
last_element = possible_sequence[-1]
if (
last_element % 3 == 0
and last_element // 3 in lookup
and lookup[last_element // 3] >= 1
):
lookup[last_element // 3] -= 1
possible_sequence.append(last_element // 3)
if found_sequence(possible_sequence, lookup):
return True
if last_element * 2 in lookup and lookup[last_element * 2] >= 1:
lookup[last_element * 2] -= 1
possible_sequence.append(last_element * 2)
if found_sequence(possible_sequence, lookup):
return True
return False
n = int(input())
given_sequence = list(map(int, input().split()))
lookup = {}
result = []
for element in given_sequence:
if element in lookup:
lookup[element] += 1
else:
lookup[element] = 1
for index in range(len(given_sequence)):
if found_sequence([given_sequence[index]], dict(lookup)):
break
|
FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN 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 DICT ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR LIST VAR VAR 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()))
b = [[a[i], 0, 0] for i in range(n)]
for i in range(n):
x = a[i]
while x % 2 == 0:
x = x // 2
b[i][1] += 1
while x % 3 == 0:
x = x // 3
b[i][2] += 1
b.sort(key=lambda x: (x[1], -x[2]))
c = [b[i][0] for i in range(n)]
print(*c)
|
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 VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER 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$.
|
n = int(input())
arr = []
for ai in list(map(int, input().split())):
num, cnt = ai, 0
while ai % 3 == 0:
ai //= 3
cnt += 1
arr.append([-cnt, num])
arr.sort()
print(" ".join(list(map(lambda x: str(x[1]), arr))))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER 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$.
|
def main():
n, sequence = input(), set(map(int, input().split()))
firstElement = nThrees = 0
for element in sequence:
value = element
for i in range(39):
if value % 3:
if nThrees < i:
firstElement, nThrees = element, i
break
value //= 3
if not nThrees:
return " ".join(map(str, sorted(sequence)))
return rearrangeElements(sequence, firstElement)
def rearrangeElements(sequence, firstElement):
newSequence = ""
if not firstElement % 2:
elementBeforeFirst = firstElement // 2
while elementBeforeFirst in sequence:
newSequence = f" {elementBeforeFirst}" + newSequence
elementBeforeFirst //= 2
while True:
if firstElement not in sequence:
return newSequence[1:]
while firstElement in sequence:
newSequence += f" {firstElement}"
firstElement *= 2
firstElement //= 6
print(main())
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR RETURN FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR STRING IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR VAR NUMBER WHILE NUMBER IF VAR VAR RETURN VAR NUMBER WHILE VAR VAR VAR STRING VAR VAR NUMBER VAR NUMBER 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$.
|
def GetCount3(k):
count = 0
while k % 3 == 0:
k //= 3
count += 1
return count
n = input()
l = sorted(map(int, input().split()), key=lambda a: (-GetCount3(a), a))
print(*l)
|
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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$.
|
def buildIt(prevval, a, used, howmanyused):
if howmanyused == len(a):
return [prevval]
for i in range(len(a)):
if used[i] == False:
if a[i] == prevval * 2:
used[i] = True
howmanyused += 1
answer = buildIt(a[i], a, used, howmanyused)
if answer != None:
return [prevval] + answer
else:
used[i] = False
howmanyused -= 1
if prevval % 3 == 0 and a[i] == prevval // 3:
used[i] = True
howmanyused += 1
answer = buildIt(a[i], a, used, howmanyused)
if answer != None:
return [prevval] + answer
else:
used[i] = False
howmanyused -= 1
return None
n = int(input())
a = [int(x) for x in input().split()]
used = [(False) for x in a]
howmany = 0
for i in range(len(a)):
startval = a[i]
used[i] = True
howmanyused = 1
tryit = buildIt(startval, a, used, howmanyused)
if tryit != None:
for x in tryit:
print(x, end=" ")
break
else:
used[i] = False
|
FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN LIST VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NONE RETURN BIN_OP LIST VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NONE RETURN BIN_OP LIST VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NONE FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN 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(""))
arr = list(map(int, input().split()))
arr.sort()
sol = []
for i in range(n):
sol.append([0, arr[i]])
for i in range(n):
cnt = 0
while arr[i] % 3 == 0:
cnt += 1
arr[i] = arr[i] // 3
sol[i][0] = cnt
sol = sorted(sol, key=lambda x: x[0], reverse=True)
ans = []
for i in range(n):
ans.append(sol[i][1])
print(*ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING 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 EXPR FUNC_CALL VAR LIST NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER 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$.
|
n = int(input())
a = list(map(int, input().split()))
def d3(c):
ans = 0
while c % 3 == 0:
c //= 3
ans += 1
return ans
a = [(d3(i), i) for i in a]
a = sorted(a, key=lambda x: (-x[0], x[1]))
print(" ".join([str(i[1]) for i in 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 WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR 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$.
|
N = int(input())
l = []
w = []
used = 0
res = []
orig = []
l_t = list(map(int, input().split()))
for a, b in enumerate(l_t):
l.append((b, a))
orig.append((b, a))
while used < N:
tmp = [i for i in l if i[0] % 2 != 0]
w.append(tmp)
used += len(tmp)
for i in tmp:
l.remove(i)
for i in range(len(l)):
l[i] = l[i][0] // 2, l[i][1]
for i in range(len(w)):
w[i].sort(key=lambda a: a[0], reverse=True)
for i in range(len(w)):
for j in w[i]:
res.append(orig[j[1]][0])
print(*res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER 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())
arr = list(map(int, input().split()))
for i, x in enumerate(arr):
hp = 0
while not x % 3**hp:
hp += 1
arr[i] = hp, x
arr.sort(key=lambda t: (t[0], -t[1]), reverse=True)
print(*map(lambda o: o[1], arr))
|
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 FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR 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 = input().split()
ans = []
for i in range(n):
a[i] = int(a[i])
a.sort()
j = 0
while len(a) > 0:
temp = []
i = 0
while i < len(a):
if a[i] % 3 != 0:
temp.append(a[i] * pow(3, j))
a.remove(a[i])
else:
a[i] //= 3
i += 1
j += 1
ans.append(temp)
k = len(ans)
for i in range(k):
for j in range(len(ans[k - i - 1])):
print(ans[k - i - 1][j], end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER 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 v3(x):
ans = 0
while x % 3 == 0:
x //= 3
ans += 1
return ans
def v2(x):
ans = 0
while x % 2 == 0:
x //= 2
return ans
n = int(input())
arr = sorted(map(int, input().split()))
d = set()
mxpow3 = 0
pow3 = 0
mnpow2 = 1000
pow2 = 0
for num in arr:
d.add(num)
if v3(num) > mxpow3:
mxpow3 = v3(num)
pow3 = num
if v2(num) < mnpow2:
mnpow2 = v2(num)
pow2 = num
cur = 0
tot = 0
if mxpow3 == 0:
print(pow2, end=" ")
cur = pow2
tot += 1
else:
print(pow3, end=" ")
cur = pow3
tot += 1
while tot < n:
if 2 * cur in d:
cur = 2 * cur
print(cur, end=" ")
else:
cur //= 3
print(cur, end=" ")
tot += 1
|
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER 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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR IF BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR 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()))
def search(partial, current_value, remaining):
if not partial:
partial = [current_value]
if len(remaining) == 0:
return partial
res = []
new_values = [current_value * 2]
if current_value % 3 == 0:
new_values.append(current_value // 3)
for new_value in new_values:
if new_value in remaining:
i = remaining.index(new_value)
for answer in search(
partial + [remaining[i]], new_value, remaining[:i] + remaining[i + 1 :]
):
if answer:
res.append(answer)
return res
for idx, elem in enumerate(l):
ans = search(None, elem, l[:idx] + l[idx + 1 :])
if len(ans) > 0:
print(" ".join(map(str, ans)))
break
|
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 VAR ASSIGN VAR LIST VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR LIST BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NONE VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR 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 three(x):
c = 0
while x % 3 == 0:
x //= 3
c += 1
return c
def two(x):
c = 0
while x % 2 == 0:
x //= 2
c += 1
return c
n = int(input())
a = list(map(int, input().split()))
D = [[0, 0, 0] for i in range(n)]
for i in range(n):
D[i][0] = a[i]
D[i][1] = three(a[i])
D[i][2] = two(a[i])
for j in range(len(D) - 1):
for i in range(len(D) - 1):
if D[i][1] < D[i + 1][1]:
D[i], D[i + 1] = D[i + 1], D[i]
for j in range(len(D) - 1):
for i in range(len(D) - 1):
if D[i][2] > D[i + 1][2]:
D[i], D[i + 1] = D[i + 1], D[i]
for i in D:
print(i[0], end=" ")
|
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF 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 NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER 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$.
|
n = int(input())
data = map(int, input().split())
mass = []
for el in data:
save = el
th_deg = 0
tw_deg = 0
while el % 3 == 0:
el //= 3
th_deg += 1
while el % 2 == 0:
el //= 2
tw_deg += 1
mass.append((save, tw_deg, th_deg))
def comp(a, b):
if a[2] > b[2]:
return False
if a[2] < b[2]:
return True
return not a[1] < b[1]
for i in range(n):
for j in range(i + 1, n):
if comp(mass[i], mass[j]):
mass[i], mass[j] = mass[j], mass[i]
for el in mass:
print(el[0], 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 ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR 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 isThird(x, y):
return x == y * 3
def isDouble(x, y):
return x == y / 2
def sort(lista):
if len(lista) == 0:
return []
lista = sorted(lista)
result = []
n = len(lista)
result.append(lista.pop(0))
p = -1
while len(result) < n:
p = (p + 1) % len(lista)
if lista[p] == result[0] * 3 or lista[p] == result[0] / 2:
result.insert(0, lista.pop(p))
elif lista[p] == result[-1] * 2 or lista[p] == result[-1] / 3:
result.append(lista.pop(p))
return result
def main():
n = int(input())
numeros = input().split()
numeros = [int(x) for x in numeros]
mult6 = []
mult3 = []
mult2 = []
none = []
for num in numeros:
if num % 6 == 0:
mult6.append(num)
elif num % 2 == 0:
mult2.append(num)
elif num % 3 == 0:
mult3.append(num)
else:
none.append(num)
mult3 = sorted(mult3, reverse=True)
mult6 = sort(mult6)
mult2 = sorted(mult2)
final = mult3 + mult6 + none + mult2
for num in final:
print(num, end=" ")
print()
main()
|
FUNC_DEF RETURN VAR BIN_OP VAR NUMBER FUNC_DEF RETURN VAR BIN_OP VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF 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 LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL 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())
u = list(map(int, input().split()))
ok = False
def finl(u, d):
for i in range(len(u)):
if u[i] == d:
return i
return -1
def gen(i):
global ans
if len(ans) == n:
for i in ans:
print(i, end=" ")
exit()
p = finl(u, u[i] * 2)
if p != -1:
ans.append(u[p])
gen(p)
ans.pop()
if u[i] % 3 == 0:
p = finl(u, u[i] // 3)
if p != -1:
ans.append(u[p])
gen(p)
ans.pop()
for i in range(n):
ans = [u[i]]
gen(i)
|
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 FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST 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())
s = list(map(int, input().split()))
sett = set(s)
last = s[0]
while True:
if last % 3 == 0 and last // 3 in sett:
last = last // 3
elif last * 2 in sett:
last = last * 2
else:
break
arr = [last]
while True:
if last % 2 == 0 and last // 2 in sett:
last = last // 2
arr += [last]
elif last * 3 in sett:
last = last * 3
arr += [last]
else:
break
arr = arr[::-1]
print(" ".join(list(map(str, arr))))
|
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 VAR ASSIGN VAR VAR NUMBER WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR LIST VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR LIST VAR ASSIGN VAR VAR NUMBER 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())
a = [int(x) for x in input().split()]
def fact(x):
c2 = 0
c3 = 0
while x % 2 == 0:
x = x // 2
c2 = c2 + 1
while x % 3 == 0:
x = x // 3
c3 = c3 + 1
return c2, c3, x
b = [fact(x) for x in a]
b.sort(key=lambda x: (x[0], -x[1]))
print(" ".join([str(2**c2 * 3**c3 * k) for c2, c3, k in b]))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR 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 RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR 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$.
|
def dfs(p, n):
ans.append(a[p])
v[p] = True
if len(ans) == n:
for i in ans:
print(i, end=" ")
exit()
for j in range(0, n):
if v[j] == False and (a[j] * 3 == ans[-1] or 2 * ans[-1] == a[j]):
dfs(j, n)
else:
continue
return
n = int(input())
a = list(map(int, input().split()))
for i in range(0, n):
ans = list()
v = [False] * n
dfs(i, n)
|
FUNC_DEF EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN 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 NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER 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$.
|
R = lambda: list(map(int, input().split(" ")))
n = int(input())
a = R()
answer = [a[0]]
while len(answer) < n:
for i in range(n):
if a[i] * 2 == answer[0] or answer[0] * 3 == a[i]:
answer = [a[i]] + answer
elif answer[-1] * 2 == a[i] or a[i] * 3 == answer[-1]:
answer += [a[i]]
print(*answer)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER WHILE FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR LIST 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$.
|
input()
d = set(list(map(int, input().strip().split())))
x = min(d)
d.remove(x)
sol = [x]
while d:
if x * 2 in d:
x = x * 2
sol.append(x)
d.remove(x)
elif x % 3 == 0 and x // 3 in d:
x = x // 3
sol.append(x)
d.remove(x)
else:
break
sol2 = []
x = sol[0]
while d:
if x * 3 in d:
x = x * 3
sol2.append(x)
d.remove(x)
elif x % 2 == 0 and x // 2 in d:
x = x // 2
sol2.append(x)
d.remove(x)
sol = list(reversed(sol2)) + sol
print(" ".join(map(str, sol)))
|
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR WHILE VAR IF 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 NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER WHILE VAR IF 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 NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR 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$.
|
n = int(input())
l = list(map(int, input().split()))
def fact(n):
m = n
c, c1 = 0, 0
while n % 2 == 0:
n = n // 2
c += 1
while n % 3 == 0:
n = n // 3
c1 += 1
return c1, -c, m
l1 = []
for i in l:
l1.append(fact(i))
l1.sort(reverse=True)
for i in range(n):
print(l1[i][2], 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 VAR ASSIGN VAR VAR NUMBER 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 LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER 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())
l = [int(x) for x in input().split()]
def key(x, r=0):
while x % 3 == 0:
x //= 3
r -= 1
while x % 2 == 0:
x //= 2
r += 1
return r
print(*sorted(l, key=key))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF 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 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 m3(n):
cnt = 0
while n % 3 == 0:
cnt += 1
n //= 3
return cnt
n = int(input())
l = list(map(int, input().split()))
a = []
for i in range(n):
x = m3(l[i])
a.append((-x, l[i]))
a.sort()
for i in range(n):
print(a[i][1], end=" ")
|
FUNC_DEF 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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR 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$.
|
def div(a, l):
if a % 3 == 0:
x = a // 3
else:
return False
if x in l:
return True
else:
return False
def mul(a, l):
x = a * 2
if x in l:
return True
else:
return False
n = int(input())
l = list(map(int, input().split()))
for i in l:
p = []
p.extend(l)
ans = []
ans.append(i)
p.remove(i)
start = i
flag = 0
while len(p) != 0:
if div(start, p):
ans.append(start // 3)
p.remove(start // 3)
start = start // 3
elif mul(start, p):
ans.append(start * 2)
p.remove(start * 2)
start = start * 2
else:
break
if len(ans) == n:
flag = 1
break
print(*ans)
|
FUNC_DEF IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN 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())
mas = input().split()
s = set()
for i, x in enumerate(mas):
mas[i] = int(x)
s.add(mas[i])
start = -1
for x in mas:
if not (x % 2 == 0 and x // 2 in s or x * 3 in s):
start = x
break
i = 1
ans = [start]
while i < n:
if start * 2 in s:
ans.append(start * 2)
start = start * 2
i += 1
else:
ans.append(start // 3)
start = start // 3
i += 1
print(" ".join(str(i) for i in ans))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER 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$.
|
n = int(input())
ls = list(map(int, input().split()))
cnt2 = [0] * n
cnt3 = [0] * n
for i in range(n):
temp = ls[i]
while temp % 2 == 0:
temp = temp // 2
cnt2[i] += 1
temp = ls[i]
while temp % 3 == 0:
temp = temp // 3
cnt3[i] += 1
for i in range(n):
for j in range(n - i - 1):
if cnt2[j] > cnt2[j + 1]:
cnt2[j], cnt2[j + 1] = cnt2[j + 1], cnt2[j]
cnt3[j], cnt3[j + 1] = cnt3[j + 1], cnt3[j]
ls[j], ls[j + 1] = ls[j + 1], ls[j]
for i in range(n):
for j in range(n - i - 1):
if cnt3[j] < cnt3[j + 1]:
cnt2[j], cnt2[j + 1] = cnt2[j + 1], cnt2[j]
cnt3[j], cnt3[j + 1] = cnt3[j + 1], cnt3[j]
ls[j], ls[j + 1] = ls[j + 1], ls[j]
for i in ls:
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 ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR 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 ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR 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 ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER 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$.
|
n = int(input())
def foo(p, a):
if not a:
return [p]
for i in a:
if not p or p % 3 == 0 and p // 3 == i or p * 2 == i:
result = foo(i, a.difference({i}))
if result is not None:
return ([p] if p else []) + result
print(*foo(0, set(map(int, input().split()))))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR RETURN LIST VAR FOR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NONE RETURN BIN_OP VAR LIST VAR LIST VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR 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$.
|
def deg(x):
ret = 0
while x % 3 == 0:
ret += 1
x //= 3
return ret
n = int(input())
a = [int(i) for i in input().split()]
res = []
for i in a:
res.append([-deg(i), i])
res.sort()
for i in res:
print(i[1], end=" ")
print()
|
FUNC_DEF 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL 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$.
|
from sys import stdin, stdout
def rint():
return map(int, stdin.readline().split())
def comp(x):
i = 0
tmp = x
while tmp % 3 == 0:
tmp //= 3
i += 1
n3 = i
i = 0
tmp = x
while tmp % 2 == 0:
tmp //= 2
i += 1
n2 = i
return -n3 * 100 + n2
n = int(input())
a = list(rint())
a.sort(key=comp)
print(*a)
|
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL 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$.
|
import sys
n = int(sys.stdin.readline().strip())
l2 = sys.stdin.readline().strip().split(" ")
l = [int(i) for i in l2]
dic = {i for i in l}
pairing = {}
sequence = []
for i in range(n):
temp = [[], []]
num = l[i]
f1 = num * 3
if num % 2 == 0:
f2 = num // 2
if f2 in dic:
temp[0].append(f2)
b1 = num * 2
if num % 3 == 0:
b2 = num // 3
if b2 in dic:
temp[1].append(b2)
if f1 in dic:
temp[0].append(f1)
if b1 in dic:
temp[1].append(b1)
pairing[l[i]] = temp
bs = []
for key in pairing:
f = pairing[key][0]
b = pairing[key][1]
if len(b) != 0:
for item in b:
bs.append(item)
for front in f:
if key in pairing[front][1]:
sequence.append((front, key))
head = [i for i in l if i not in bs]
p = head[0]
output = ""
for i in range(n - 1):
output += str(p) + " "
temp = [x for x in sequence if x[0] == p]
nextt = temp[0][1]
p = nextt
if i == n - 2:
output += str(p)
print(output)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST LIST ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER 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$.
|
def tri(a):
k = 0
while a % 3 == 0:
k += 1
a //= 3
return k
n = input()
katya = list(map(int, input().split()))
l = [(-tri(x), x) for x in katya]
p = [x[1] for x in sorted(l)]
print(*p)
|
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER 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$.
|
def f(x):
i = 0
z = x
while z % 3 == 0:
z //= 3
i += 1
return i
n = int(input())
a = list(map(int, input().split()))
x = [(-f(a[i]), a[i]) for i in range(n)]
x.sort()
print(" ".join([str(x[i][1]) for i in range(n)]))
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR 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 FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER VAR 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, l = int(input()), list(map(int, input().split()))
for i in l:
a, x = [i], l.copy()
x.remove(i)
while len(a) != n:
p, q = a[-1] // 3, a[-1] * 2
if a[-1] % 3 == 0 and p in x:
a.append(p)
x.remove(p)
elif q in x:
a.append(q)
x.remove(q)
else:
break
if len(a) == n:
exit(print(*a))
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR 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()]
i = 0
B = A.copy()
C = [1000000000.0]
D = B.copy()
while i < 39:
while max(D) != 0:
C = [(x // 3) for x in D]
D = C.copy()
i = i + 1
break
k = i - 1
R = set(B.copy())
X = [x for x in list(R) if x % 3**k == 0]
L = list({})
A = []
while k >= 0:
X = [x for x in list(R) if x % 3**k == 0]
X.sort()
L.extend(X)
R = R.difference(set(X))
k = k - 1
print(" ".join([str(x) for x in L]))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR DICT ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP 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$.
|
n = int(input())
a = list(map(int, input().split()))
b = [0] * n
for i in range(n):
x = a[i]
y = 0
while x % 3 == 0:
y += 1
x = x // 3
b[i] = y
d = []
maxi = max(b)
while maxi > -1:
c = []
for i in range(n):
if b[i] == maxi:
c.append(a[i])
c = sorted(c)
for i in range(len(c)):
d.append(c[i])
maxi -= 1
s = ""
for i in range(len(d)):
s += str(d[i]) + " "
print(s)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR STRING 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 deg_3(x):
deg = 0
while x % 3 == 0:
x //= 3
deg += 1
return deg
n = int(input())
a = map(int, input().split())
a_deg = sorted([(a_i, deg_3(a_i)) for a_i in a], key=lambda x: (-x[1], x[0]))
print(" ".join([str(val) for val, deg in a_deg]))
|
FUNC_DEF 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR 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$.
|
def f(x, k):
cnt = 0
while x % k == 0:
x = x // k
cnt += 1
return cnt
n = int(input())
a = map(int, input().split())
print(*sorted(a, key=lambda x: [f(x, 2), -f(x, 3)]))
|
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR LIST FUNC_CALL VAR VAR NUMBER 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$.
|
import sys
data = sys.stdin.readlines()
lis = []
for line in data:
lis.append(line)
n = lis[0].split()[0]
n = int(n)
seq = list(map(int, lis[1].split()))
def e2e3(n):
n1 = n
bn = bin(n)[2:]
e2 = len(bn) - (bn.rfind("1") + 1)
e3 = 0
n = n >> e2
while n % 3 == 0:
n = n // 3
e3 += 1
return e2, -e3, n1
seq = [e2e3(n) for n in seq]
seq.sort()
for n in seq:
print(n[-1], end=" ")
|
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR 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$.
|
import sys
n = int(input())
seq = list(map(int, input().split()))
used = [False] * n
def A_n_k_pythonic(a, curr, depth=0):
if depth == len(a):
print(" ".join(map(str, curr)))
sys.exit()
for i in range(len(a)):
if not used[i]:
if not curr or curr[-1] == a[i] * 3 or curr[-1] * 2 == a[i]:
curr.append(a[i])
used[i] = True
A_n_k_pythonic(a, curr, depth + 1)
curr.pop()
used[i] = False
A_n_k_pythonic(seq, [])
|
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 BIN_OP LIST NUMBER VAR FUNC_DEF NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER 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$.
|
def get(x):
a = x
cnt = [0] * 2
for d in (2, 3):
while x % d == 0:
cnt[d - 2] += 1
x //= d
return cnt[0], -cnt[1], a
(n,) = map(int, input().split())
a = list(map(int, input().split()))
values = sorted([get(x) for x in a])
print(*[x[2] for x in values])
|
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR RETURN VAR NUMBER VAR NUMBER 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 VAR 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$.
|
n = int(input())
lista = list(map(int, input().split()))
def defe(x):
i = 0
while x % 2 == 0:
i += 1
x /= 2
return -x, i
lista.sort(key=defe)
print(" ".join(list(map(str, lista))))
|
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 VAR EXPR FUNC_CALL 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$.
|
def dfs(value, count, stop):
global mySet
global collect
global check
if count == stop:
return True
if value % 3 == 0:
if value // 3 in mySet:
count += 1
collect.append(value // 3)
check = dfs(value // 3, count, stop)
if not check:
count -= 1
if not check:
if value * 2 in mySet:
count += 1
collect.append(value * 2)
check = dfs(value * 2, count, stop)
if not check:
count -= 1
return check
size = int(input())
collect = []
check = False
mySet = set(map(int, input().split()))
for k in mySet:
collect.append(k)
if dfs(k, 1, size):
break
else:
collect = []
print(" ".join(str(i) for i in collect))
|
FUNC_DEF IF VAR VAR RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER IF VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST 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())
a = list(map(int, input().split()))
b = []
for i in range(n):
for k in a:
if not (k * 2 in a or k // 3 in a and k % 3 == 0):
a.remove(k)
b.append(k)
print(*reversed(b))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR 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().strip().split()))
l = []
for i in a:
k = 0
k = i
c = 0
while k % 3 == 0:
k = k // 3
c += 1
p = -c
l.append([p, i])
l.sort()
res = []
for i in l:
res.append(i[1])
print(" ".join(map(str, res)))
|
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 VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR 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$.
|
n = int(input())
arr = list(map(int, input().split()))
ans = [arr[0]]
front = False
for _ in range(n - 1):
if not front:
if ans[-1] * 2 in arr:
ans.append(ans[-1] * 2)
elif ans[-1] % 3 == 0 and ans[-1] // 3 in arr:
ans.append(ans[-1] // 3)
else:
front = True
if front:
if ans[0] * 3 in arr:
ans = [ans[0] * 3] + ans
elif ans[0] % 2 == 0 and ans[0] // 2 in arr:
ans = [ans[0] // 2] + ans
print(" ".join([str(i) for i in ans]))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR IF BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR IF BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER NUMBER VAR IF BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER NUMBER 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$.
|
x = int(input())
temp_seq = input().split(" ")
seq = [int(temp_seq[i]) for i in range(x)]
final = []
while len(seq) != 0:
if len(final) == 0:
final = final + [seq[0]]
seq.pop(0)
elif final[0] // 2 in seq:
final = [final[0] // 2] + final
seq.remove(final[0])
elif final[-1] * 2 in seq:
final = final + [final[-1] * 2]
seq.remove(final[-1])
elif final[0] * 3 in seq:
final = [final[0] * 3] + final
seq.remove(final[0])
elif final[-1] % 3 == 0 and final[-1] // 3 in seq:
final = final + [final[-1] // 3]
seq.remove(final[-1])
res = " ".join(str(a) for a in final)
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR LIST VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR LIST BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR LIST BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING 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$.
|
def pow3(x):
c = 0
while x:
c += 1
if x % 3 == 0:
x = x // 3
else:
x = 0
return -1 * c
N = int(input())
s = [(int(x), pow3(int(x))) for x in input().split()]
s.sort(key=lambda x: (x[1], x[0]))
for i in s:
print(i[0], end=" ")
print()
|
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL 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$.
|
def func(num):
cnt = 0
while num % 3 == 0:
num //= 3
cnt += 1
return cnt
n = int(input())
arr = list(map(int, input().split()))
d = {}
val = []
for i in arr:
x = func(i)
if x in d:
d[x].append(i)
else:
d[x] = [i]
val.append(x)
val.sort()
val = val[-1::-1]
ans = []
for i in val:
temp = d[i]
temp.sort()
ans += temp
print(*ans)
|
FUNC_DEF 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 DICT ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN 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$.
|
input()
numbers = set(map(int, input().split()))
result = []
for n in numbers:
if n * 2 not in numbers and (n % 3 != 0 or n // 3 not in numbers):
result.append(n)
numbers.remove(n)
break
while numbers:
if result[0] % 2 == 0:
candidate = result[0] // 2
if candidate in numbers:
numbers.remove(candidate)
result.insert(0, candidate)
continue
candidate = result[0] * 3
if candidate in numbers:
numbers.remove(candidate)
result.insert(0, candidate)
print(" ".join(str(x) for x in result))
|
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST 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 WHILE VAR IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER 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$.
|
def poly_check(nums):
for i in range(len(nums) - 1):
if nums[i + 1] // 2 != nums[i] and nums[i + 1] * 3 != nums[i]:
return False
return True
def rec_poly(fr, to):
if len(fr) == 0:
if poly_check(to):
for n in to:
print(n, end=" ")
first = to[0]
if first * 3 in fr or first // 2 in fr:
if first * 3 in fr:
to.insert(0, first * 3)
fr.remove(first * 3)
rec_poly(fr[:], to[:])
if first // 2 in fr:
to.insert(0, first // 2)
fr.remove(first // 2)
rec_poly(fr[:], to[:])
else:
return
n = int(input())
ns = list(map(int, input().split()))
ps = []
not3 = sorted([n for n in ns if n % 3 != 0])
for n in not3:
ps.append(n)
ns.remove(n)
if ps:
rec_poly(ns[:], ps[:])
else:
for n in ns:
nns = ns[:]
pps = ps[:]
nns.remove(n)
pps.append(n)
rec_poly(nns, pps)
|
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN 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 FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL 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$.
|
n = int(input())
arr = list(map(int, input().split()))
ans = [arr[0]]
stack = [arr[0]]
visited = set()
while stack:
p = stack.pop()
visited.add(p)
if p * 2 in arr and p * 2 not in visited:
ans.append(p * 2)
stack.append(p * 2)
if p % 3 == 0 and p // 3 in arr and p // 3 not in visited:
ans.append(p // 3)
stack.append(p // 3)
if p * 3 in arr and p * 3 not in visited:
ans.insert(0, p * 3)
stack.append(p * 3)
if p % 2 == 0 and p // 2 in arr and p // 2 not in visited:
ans.insert(0, p // 2)
stack.append(p // 2)
print(*ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR 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 NUMBER BIN_OP VAR NUMBER VAR 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 BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL 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$.
|
n = int(input())
A = list(map(int, input().split()))
mul2 = [0] * n
div3 = [0] * n
for i in range(n):
if A[i] * 2 in A:
mul2[i] = 1
if A[i] % 3 == 0 and A[i] // 3 in A:
div3[i] = 1
index = -1
for i in range(n):
if mul2[i] == 0 and div3[i] == 0:
index = i
break
ans = [0] * n
visited = [0] * n
visited[index] = 1
ans[n - 1] = A[index]
for i in range(n - 2, -1, -1):
if ans[i + 1] * 3 in A and visited[A.index(ans[i + 1] * 3)] == 0:
ans[i] = ans[i + 1] * 3
elif ans[i + 1] // 2 in A and visited[A.index(ans[i + 1] // 2)] == 0:
ans[i] = ans[i + 1] // 2
for i in range(n):
print(ans[i], 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 BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR 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$.
|
import sys
n = int(input())
a = list(map(int, input().split()))
def dfs(u):
r.append(u)
s.remove(u)
if len(r) == n:
print(*r)
exit(0)
if u % 3 == 0 and u // 3 in s:
dfs(u // 3)
if u * 2 in s:
dfs(u * 2)
for i in range(n):
s = set(a)
r = []
dfs(a[i])
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST 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())
a = list(map(int, input().split()))
ok = False
a.sort()
def search(t):
l = 0
r = n - 1
while r - l >= 0:
mid = (r + l) // 2
if a[mid] == t:
return mid
if t < a[mid]:
r = mid - 1
if t > a[mid]:
l = mid + 1
return -1
if n == 1:
print(a[0])
else:
for i in range(n):
q = [[[a[i]], [0] * n]]
while q:
e = q.pop()
ne = e[0]
v = e[1]
v[i] = 1
if len(ne) == n:
print(" ".join(map(str, ne)))
ok = True
break
if (
ne[-1] % 3 == 0
and search(ne[-1] // 3) >= 0
and v[search(ne[-1] // 3)] == 0
):
nv = v.copy()
nv[search(ne[-1] // 3)] = 1
q.append([ne + [ne[-1] // 3], nv])
if search(ne[-1] * 2) >= 0 and v[search(ne[-1] * 2)] == 0:
nv = v.copy()
nv[search(ne[-1] * 2)] = 1
q.append([ne + [ne[-1] * 2], nv])
if ok:
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 NUMBER EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST LIST VAR VAR BIN_OP LIST NUMBER VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR LIST BIN_OP VAR NUMBER NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR LIST BIN_OP VAR NUMBER NUMBER VAR IF 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$.
|
int(input())
a = list(map(int, input().split()))
ra = [a[0]]
a.pop(0)
while len(a):
f = -1
ff = -1
for i in range(len(a)):
if a[i] * 2 == ra[0]:
f = i
ff = 0
break
elif ra[-1] * 2 == a[i]:
f = i
ff = 1
break
elif a[i] % 3 == 0 and a[i] // 3 == ra[0]:
f = i
ff = 0
break
elif ra[-1] % 3 == 0 and ra[-1] // 3 == a[i]:
f = i
ff = 1
break
if ff == 0:
ra = [a[f]] + ra
else:
ra = ra + [a[f]]
a.pop(f)
for i in ra:
print(i, end=" ")
|
EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR VAR ASSIGN VAR BIN_OP VAR LIST VAR VAR 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$.
|
n = int(input())
a = list(map(int, input().split()))
ans = []
while len(a) > 0:
for k in range(38, -1, -1):
subans = []
for x in a[:]:
if x % 3**k == 0:
subans = subans + [x]
a.remove(x)
ans = ans + sorted(subans)
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 WHILE FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP 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$.
|
n = int(input())
l = [int(e) for e in input().split()]
dic = {}
for i in l:
if i in dic:
dic[i] += 1
else:
dic[i] = 1
res = []
def dfs(res):
temp = res[-1]
tempLen = len(res)
if temp % 3 == 0:
tempDiv = temp // 3
if tempDiv in dic and dic[tempDiv] > 0:
res.append(tempDiv)
dic[tempDiv] -= 1
dfs(res)
if len(res) == n:
return res
else:
res = res[0:tempLen]
dic[tempDiv] += 1
temp = res[-1]
tempDiv = temp * 2
if tempDiv in dic and dic[tempDiv] > 0:
res.append(tempDiv)
dic[tempDiv] -= 1
dfs(res)
if len(res) == n:
return res
else:
res = res[0:tempLen]
dic[tempDiv] += 1
temp = res[-1]
return res
for i in range(0, n):
res = [l[i]]
res = dfs(res)
if len(res) == n:
break
for i in res:
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 DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR 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$.
|
n = int(input())
s = list(map(int, input().split()))
ans = []
for i in s:
n = i
c2 = c3 = 0
while i % 2 == 0:
i //= 2
c2 += 1
while i % 3 == 0:
i //= 3
c3 += 1
ans.append([-c3, c2, n])
ans.sort()
for i in ans:
print(i[2], 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 VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST 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$.
|
n = input()
c = {}
for x in [int(x) for x in input().split()]:
u, t = x, 0
while 0 == u % 3:
u //= 3
t += 1
c[t] = c.get(t, []) + [x]
for x in sorted(c)[::-1]:
for y in sorted(c[x]):
print(y, end=" ")
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER WHILE NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR LIST LIST VAR FOR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL 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$.
|
def maxpower(n):
if n % 3 != 0:
return 0
else:
count = 0
while n % 3 == 0:
n = n // 3
count += 1
return count
def mykey(n):
return -1 * maxpower(n), n
n = int(input())
L = list(map(int, input().split()))
print(*sorted(L, key=mykey))
|
FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP NUMBER 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 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 = [int(i) for i in input().split(" ")]
path = []
for p in range(len(a)):
path.append(-1)
b = []
for i in range(len(a)):
b.append([-1, -1])
for x in range(len(a)):
mul_two = a[x] * 2
done = 0
if a[x] % 3 == 0:
done = 1
div_thr = a[x] // 3
for y in range(len(a)):
if a[y] == mul_two:
b[x][0] = y
break
elif done == 1:
if a[y] == div_thr:
b[x][1] = y
break
for i in range(len(a)):
cond = True
k = i
count = 0
ans = 0
path_var = 0
for x in range(len(a)):
path[x] = -1
path[path_var] = a[i]
path_var += 1
while cond == True:
if b[k][0] != -1:
k = b[k][0]
count += 1
path[path_var] = a[k]
path_var += 1
if count == len(a) - 1:
ans = 1
break
elif b[k][1] != -1:
k = b[k][1]
path[path_var] = a[k]
path_var += 1
count += 1
if count == len(a) - 1:
ans = 1
break
else:
break
if ans == 1:
break
for i in range(len(path)):
print(path[i], end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF 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$.
|
n = int(input())
l = [int(x) for x in input().split()]
for ele in l:
if ele % 2 == 0:
if ele // 2 not in l and ele * 3 not in l:
start = ele
elif ele * 3 not in l:
start = ele
while True:
print(start, end=" ")
if start * 2 in l:
start *= 2
continue
if start % 3 == 0 and start // 3 in l:
start = start // 3
continue
break
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR WHILE NUMBER EXPR FUNC_CALL VAR VAR STRING IF BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER 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$.
|
x = []
vis = None
def solve(u):
vis[u] = 1
x.append(A[u])
if len(x) == n:
return True
for v in range(n):
if v != u and not vis[v]:
if A[v] == 2 * A[u]:
if solve(v):
return True
if A[u] % 3 == 0 and A[v] == A[u] // 3:
if solve(v):
return True
vis[u] = 0
x.pop()
return False
n = int(input())
A = list(map(int, input().split()))
vis = [(0) for _ in range(n)]
for i in range(n):
if solve(i):
print(" ".join(map(str, x)))
break
|
ASSIGN VAR LIST ASSIGN VAR NONE FUNC_DEF ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR BIN_OP NUMBER VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN 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 NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL 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$.
|
from sys import stdin, stdout
cin = stdin.readline
cout = stdout.write
def test(k):
try:
val = d[k]
return 1
except KeyError as e:
return 0
n = int(cin())
d = {i: (1) for i in list(map(int, cin().split()))}
l = [0] * n
for i in d:
l[0] = i
for j in range(n - 1):
if l[j] % 3 == 0 and test(l[j] // 3):
l[j + 1] = l[j] // 3
elif test(l[j] * 2):
l[j + 1] = l[j] * 2
else:
break
else:
for j in l:
cout(str(j) + " ")
|
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR RETURN NUMBER VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER 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 FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER 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())
arr = list(map(int, input().split()))
arr1 = []
for i in range(n):
if arr[i] % 3 == 0:
if arr[i] // 3 in arr:
arr1.append([arr[i], arr[i] // 3])
if arr[i] * 2 in arr:
arr1.append([arr[i], arr[i] * 2])
arr1 = sorted(arr1)
ans = [arr1[0][0], arr1[0][1]]
x = arr1[0][0]
y = arr1[0][1]
while len(ans) != n:
for i in range(1, len(arr1)):
if arr1[i][0] == y:
y = arr1[i][1]
ans.append(arr1[i][1])
elif arr1[i][1] == x:
x = arr1[i][0]
ans.insert(0, arr1[i][0])
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 IF BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER 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 rec(x, fArr, secArr):
fArr.remove(x)
secArr.append(x)
if 2 * x in fArr:
return rec(2 * x, fArr, secArr)
elif x % 3 == 0 and x // 3 in fArr:
return rec(x // 3, fArr, secArr)
elif len(fArr) == 0:
return secArr
else:
return []
n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
b = rec(a[i], a.copy(), [])
if b != []:
break
print(*b)
|
FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR VAR RETURN FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN 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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR LIST IF VAR LIST 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())
arr = list(map(int, input().split()))
ans = []
for i in range(n):
p3 = 0
t = arr[i]
while t % 3 == 0:
t = t // 3
p3 += 1
p2 = 0
t = arr[i]
while t % 2 == 0:
t = t // 2
p2 += 1
ans.append([p3, -p2, arr[i]])
ans.sort(reverse=True)
for i in ans:
print(i[2], 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 WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR STRING
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.