Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, and then performs with it n−1n−1 operations 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 (2≤n≤1002≤n≤100) — the number of the elements in the sequence. The second line of the input contains nninteger numbers a1,a2,…,ana1,a2,…,an (1≤ai≤3⋅10181≤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
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][9,3,6,12,4,8]. It can match possible Polycarp's game which started with x=9x=9.

题意:

给你一个含有N个数的数组,让你重新定义他们的顺序,使之a[i] 和a[i-1] 只有两种关系

1. a[i]=a[i-1]/3

2. a[i]=a[ i-1 ] * 2

思路:

暴力DFS竟然骗过,难以置信。

正解的思路是:

我们定义一个数x的cnt3是它唯一分解后3的次幂数。

因为只有/3的操作,所以整个数组中cnt3一定是单调不递增的,

而相同的cnt3中的数,关系一定是*2的关系,那么一定是单调递增的排序。

那么我们只需要把数都压入到一个pair中,first 是这个数的cnt3,second是这个数,然后进行默认的pair排序。

默认的pair排序是,先以first递增排序,相同的first的以second递增排序。

那么我们把每一个数的cnt3取为负值,即-cnt3。

那么就直接排序就输出结果了。

细节见代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int count3(LL x){
int ret=;
while(x % == ){
ret++;
x /= ;
}
return ret;
}
int n;
vector<pair<int,LL>> v;
int main(){
cin>>n;
v.resize(n);
for(int i=; i<n; i++){
cin>>v[i].second;
v[i].first=-count3(v[i].second);
}
sort(v.begin(), v.end());
for(int i=;i<n;i++)
{
printf("%d %lld\n",v[i].first, v[i].second);
}
for(int i=; i<n; i++)
printf("%lld%c", v[i].second, " \n"[i + == n]);
}

Divide by three, multiply by two CodeForces - 977D (思维排序)的更多相关文章

  1. Codeforces Round #479 (Div. 3) D. Divide by three, multiply by two

    传送门 D. Divide by three, multiply by two •题意 给你一个数 x,有以下两种操作,x 可以任选其中一种操作得到数 y 1.如果x可以被3整除,y=x/3 2.y= ...

  2. Codeforces 997D(STL+排序)

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

  3. Codeforces 977D Divide by three, multiply by two(拓扑排序)

      Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, ...

  4. Codeforces 977D: Divide by three, multiply by two(暴力)

    题意 有nnn个无序的数,对这些数进行排列,要求ai=3×ai+1a_i=3\times a_{i+1}ai​=3×ai+1​或2×ai=ai+12\times a_i=a_{i+1}2×ai​=ai ...

  5. Codeforces Round #479 (Div. 3) D. Divide by three, multiply by two (DFS)

    题意:给你一个长度为\(n\)的序列\(a\).对它重新排列,使得\(a_{i+1}=a_{i}/3\)或\(a_{i+1}=2*a_{i}\).输出重新排列后的序列. 题解:经典DFS,遍历这个序列 ...

  6. Divide by three, multiply by two(DFS+思维)

    Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, an ...

  7. Codeforces977D ---Divide by three, multiply by two 深搜+map存出现的数

    传送门:点我 题意:给定n长度的序列,重排成后一个数是前一个数除以三,或者后一个数是前一个数乘二,要求输出这个序列. 思路:大力深搜,对每个数搜除3的和乘2的是否出现过,然后继续搜下去.如果有一个数搜 ...

  8. CF977D Divide by three, multiply by two

    题目链接 我同学在旁边做者道题,我也看了一下 真的好水难 一看这道题,直接搜索 剪枝是不可能剪枝的一辈子不可能 Code #include <cstdio> #include <io ...

  9. Codeforces 424A (思维题)

    Squats Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Statu ...

随机推荐

  1. Failed to decrypt protected XML node "DTS:Password" with error 0x8009000B "Key not valid for use in specified state.". You may not be authorized to access this information. This error occurs when t

    Question SSIS包从A服务器搬迁到B服务器,运行报错 Description: Failed to decrypt protected XML node "DTS:Password ...

  2. linux源

    系统:centos7 x86_64 一.配置本地yum源 1.1加载光驱 1.2挂载到系统 注:如果要长期使用最好把整个镜像文件拷贝到系统下 1.3配置文件 路径/etc/yum.repos.d/ 打 ...

  3. Apache kylin 入门

    本篇文章就概念.工作机制.数据备份.优势与不足4个方面详细介绍了Apache Kylin. Apache Kylin 简介 1. Apache kylin 是一个开源的海量数据分布式预处理引擎.它通过 ...

  4. margin的两个有趣现象:margin合并和margin塌陷

    margin合并 当两个元素在垂直方向并列,分别设置margin值时会发生一个margin合并的现象 举个例子,有两个div,垂直并列,box1设置margin-bottom:20px,box2设置m ...

  5. 在ubuntu18.04上安装EOS

    在ubuntu18.04上安装EOS 在ubuntu18.04上安装EOS的目的: 把交易所的eos转到eos主网,防止交易所跑路或者交易所被黑客攻击 在不联网的安全环境下,用eos官方的命令行工具, ...

  6. PHP中cURL的应用

    这里是慕课网上讲cURL 的一张图,觉得吧这个过程说的很清楚,因此就不错了. 1, 打开一个网页,下载网页内容 <?php $curl = curl_init("http://www. ...

  7. LeetCode算法题-Find All Numbers Disappeared in an Array(Java实现)

    这是悦乐书的第232次更新,第245篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第99题(顺位题号是448).给定一个整数数组,其中1≤a[i]≤n(n =数组的大小) ...

  8. LeetCode算法题-Merge Sorted Array(Java实现)

    这是悦乐书的第161次更新,第163篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第20题(顺位题号是88).给定两个排序的整数数组nums1和nums2,将nums2中 ...

  9. Linux 小知识翻译 - 「编译器和解释器」

    这次聊聊「编译器和解释器」. 编程语言中,有以C为代表的编译型语言和以Perl为代表的解释型语言.不管是哪种,程序都是以人类能够理解的形式记录的,这种形式计算机是无法理解的. 因此,才会有编译器和解释 ...

  10. Python爬虫-02:HTTPS请求与响应,以及抓包工具Fiddler的使用

    目录 1. HTTP和HTTPS 1.1. HTTP的请求和响应流程:打开一个网页的过程 1.2. URL 2. 客户端HTTP请求 3. Fiddler抓包工具的使用 3.1. 工作原理 3.2. ...