D. Divide by three, multiply by two(div3)

D. Divide by three, multiply by two
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, and then performs with it n1n−1operations of the two kinds:

  • divide the number xx by 33 (xx must be divisible by 33);
  • multiply the number xx by 22.

After each operation, Polycarp writes down the result on the board and replaces xx by the result. So there will be nn numbers on the board after all.

You are given a sequence of length nn — 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 nn (2n1002≤n≤100) — the number of the elements in the sequence. The second line of the input contains nn integer numbers a1,a2,,ana1,a2,…,an (1ai310181≤ai≤3⋅1018) — rearranged (reordered) sequence that Polycarp can wrote down on the board.

Output

Print nn 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
Copy
6
4 8 6 3 12 9
output
Copy
9 3 6 12 4 8 
input
Copy
4
42 28 84 126
output
Copy
126 42 84 28 
input
Copy
2
1000000000000000000 3000000000000000000
output
Copy
3000000000000000000 1000000000000000000 
Note

In the first example the given sequence can be rearranged in the following way: [9,3,6,12,4,8][9,3,6,12,4,8]. It can match possible Polycarp's game which started with x=9x=9.


扫描二维码关注公众号,回复: 149685 查看本文章

题意:对给的数组进行排序,要求下一个数是上一个数的两倍或者三分之一。

题解:对于a,b两个数,我们先确定a和b能被3除多少次。如果次数相同,我们优先把次数多的数排在前面(因为下一个数是上一个数的三分之一,所以应该次数多的排前面),否则,按照除完之后的数从小到大排序(除完就是2的倍数了,想要下一个数是上一个数的两倍,肯定就是小的排前面啊QWQ)。

#include<iostream>  
#include<string.h>  
#include<algorithm>  
#include<cmath>  
#include<map>  
#include<string>  
#include<stdio.h>  
#include<vector>  
#include<stack>
#include<set>
using namespace std;
#define INIT ios::sync_with_stdio(false)
#define LL long long int
bool cmp(LL a, LL b) {
	int cnta, cntb;
	cnta = cntb = 0;
	while (a % 3 == 0) {
		a /= 3;
		cnta++;
	}
	while (b % 3 == 0) {
		b /= 3;
		cntb++;
	}
	if (cnta == cntb)
		return a < b;
		return cnta > cntb;
}

int main() {
	int n;
	LL num[1005];
	while (cin >> n) {
		for (int i = 0;i < n;i++) {
			cin >> num[i];
		}
		sort(num, num + n, cmp);
		for (int i = 0;i < n;i++) {
			cout << num[i] << (i == n - 1 ? "\n" : " ");
		}
	}
	return 0;
}




猜你喜欢

转载自blog.csdn.net/swust_zeng_zhuo_k/article/details/80225577