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−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 (2≤n≤1002≤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 (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

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.

题目大意:给定的数组按照以下要求排序:后一个数是前一个数的三分之一,或者是前一个数的二倍。

思路:如果a[v]是a[u]的三分之一或者二倍,就给u->v加一条有向边,然后跑一遍拓扑排序就行了,注意得到的拓扑数组是下标

代码:

#include<cstdio>
#include<iostream>
#include<string>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>
#include<map>
typedef long long ll;
using namespace std;
const int maxn = 200000 + 100;
int G[100 +10][100 + 10];
int c[maxn];
ll topo[maxn], t;
int n; bool dfs(int u){
c[u] = -1;
for(int i = 0; i < n; i++)if(G[u][i]){
if(c[i] < 0)return false;
else if(!c[i] && !dfs(i))return false;
}
c[u] = 1;topo[--t] = u;
return true;
}
bool toposort(){
t = n;
memset(c, 0, sizeof(c));
for(int i = 0; i < n; i++)if(!c[i]){
if(!dfs(i)) return false;
}
return true;
}
int main(){
scanf("%d", &n);
ll a[maxn];
memset(G, 0, sizeof(G));
for(int i = 0; i < n; i++){
scanf("%lld", &a[i]); }
sort(a, a+n);
for(int i = 0; i < n ; i++){
int it = lower_bound(a, a+n, a[i]/3)-a;
int itt = lower_bound(a, a+n, a[i]*2)-a;
if(a[i]%3==0&&a[it]==a[i]/3)G[i][it] = 1;
if(a[i]*2==a[itt])G[i][itt] = 1; } toposort();
for(int i = 0; i < n; i++)printf("%lld ", a[topo[i]]);
}

Codeforces 977D Divide by three, multiply by two(拓扑排序)的更多相关文章

  1. Codeforces Global Round 8 E. Ski Accidents(拓扑排序)

    题目链接:https://codeforces.com/contest/1368/problem/E 题意 给出一个 $n$ 点 $m$ 边的有向图,每条边由编号较小的点通向编号较大的点,每个点的出度 ...

  2. codeforces 645 D. Robot Rapping Results Report 二分+拓扑排序

    题目链接 我们可以发现, 这是一个很明显的二分+拓扑排序.... 如何判断根据当前的点, 是否能构造出来一个唯一的拓扑序列呢. 如果有的点没有出现, 那么一定不满足. 如果在加进队列的时候, 同时加了 ...

  3. codeforces 638B—— Making Genome in Berland——————【类似拓扑排序】

    Making Genome in Berland time limit per test 1 second memory limit per test 256 megabytes input stan ...

  4. 【CodeForces 129 B】Students and Shoelaces(拓扑排序)

    Anna and Maria are in charge of the math club for junior students. When the club gathers together, t ...

  5. Codeforces Round #460 (Div. 2)_D. Substring_[dp][拓扑排序]

    题意:一个有向图,每个结点 被赋予一个小写字母,一条路径的value等与这条路径上出现次数最多的字母的数目,求该图的最大value 比赛时,用dfs超时,看官方题解用的dp和拓扑排序,a--z用0-2 ...

  6. 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= ...

  7. codeforces 792C. Divide by Three

    题目链接:codeforces 792C. Divide by Three 今天队友翻了个大神的代码来问,我又想了遍这题,感觉很好,这代码除了有点长,思路还是清晰易懂,我就加点注释存一下...分类吧. ...

  8. Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序

    B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...

  9. Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序

    C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem ...

随机推荐

  1. iscsi,nfs

    存储概述 存储的目标 存储是根据不同的应用环境通过采取合理.安全.有效的方式将数据保存到某些介质上并能保证有效的访问. 一方面它是数据临时或长期驻留的物理媒介. 另一方面,它是保证数据完整安全存放的方 ...

  2. React Native 性能优化指南【全网最全,值得收藏】

    2020 年谈 React Native,在日新月异的前端圈,可能算比较另类了.文章动笔之前我也犹豫过,但是想到写技术文章又不是赶时髦,啥新潮写啥,所以还是动笔写了这篇 React Native 性能 ...

  3. c语言一道题

    C语言中,a=b=c,a=b==c,a==(b=c),a==(b==c)有什么区别 main(){inta=1,b=2,c=3;printf("%d,%d,%d,%d\n",a=b ...

  4. Spring中使用注解 @Scheduled执行定时任务

    注解@Scheduled 使用方式 注解@Scheduled 可以作为一个触发源添加到一个方法中,例如,以下的方法将以一个固定延迟时间5秒钟调用一次执行,这个周期是以上一个调用任务的完成时间为基准,在 ...

  5. 网络流学习 - dinic

    推荐博客:https://www.cnblogs.com/SYCstudio/p/7260613.html#4246029

  6. HTTP图解笔记(六)—— 第6章 HTTP首部

    前言 为啥第一章直接跳到第六章呢,因为...博主当初看书的时候挑着看..只看了第一章和第六章┗( ▔, ▔ )┛ HTTP图解对于不熟悉HTTP的小伙伴来说是很好的书籍,建议入手! 一. HTTP报文 ...

  7. 出现An App ID with Identifier 'com.XXX.XXX’ is not available. Please enter a different string.

    解决方法: 1.移除钥匙串中的开发证书,重新导入, 完全关闭Xcode; 2.再次打开Xcode,通过 Preferences - View Details - download 新的证书: 3.选择 ...

  8. java 数组2

    一.创建异常 1.空指针异常 2.超出索引范围 二.遍历 for循环 三.求数组中的最大值 package cn.wt.day05.demon02; public class DemonArray03 ...

  9. python3迭代器

    一.前提 1.dir()函数 dir()函数带参数时,返回参数的属性和方法列表:不带参数时,返回当前范围内变量.方法和定义的类型列表 # dir(参数):带参数,返回参数的属性和方法 s = '' p ...

  10. 团队项目-Beta冲刺2

    博客介绍 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience 这个作业要求在哪里 https://w ...