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 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
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.
题意:给你一组序列,要你重新排列这一组序列,使得第i位的元素,要么是i+1位置元素的三倍,要么是i+1元素的二分之一。
一开始想简单了,就一直wa在了第四个样例。。
如果某个元素和当前元素有题意所给的关系,那么我们就给这两个元素下标连一条边。这样下来就建成了一个图。找到入度为0的位置dfs,类似于拓扑排序。这样就可以构造出那样的一组序列了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=1e2+101;
ll a[maxx];
int b[maxx];
int in[maxx];
int vis[maxx];
vector<int> p[maxx];
int n;inline void dfs(int x,int &cnt)
{for(int i=0;i<p[x].size();i++){int to=p[x][i];if(!vis[to]){b[++cnt]=to;dfs(to,cnt);}}
}
int main()
{scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(a[j]*3ll==a[i]) p[i].push_back(j),in[j]++;if(a[j]*2ll==a[i]) p[j].push_back(i),in[i]++;}}int pos;for(int i=1;i<=n;i++){if(!in[i]){pos=i;break;}}int cnt=0;b[++cnt]=pos;vis[pos]=1;dfs(pos,cnt);for(int i=1;i<=n;i++) printf("%I64d%c",a[b[i]],i==n?'\n':' ');
}

努力加油a啊,(o)/~

Divide by three, multiply by two(dfs)相关推荐

  1. 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 ...

  2. 搜索 —— 深度优先搜索(DFS)

    [概述] 深度优先搜索,是从初始状态起,利用一定的规则生成搜索树,寻找下一层任一个结点,检查是否出现目标状态,若未出现,以此状态利用规则生成再下一层任一个结点,再检查,重复过程一直到叶节点(即不能再生 ...

  3. 三十二、图的创建深度优先遍历(DFS)广度优先遍历(BFS)

    一.图的基本介绍 为什么要有图 前面我们学了线性表和树 线性表局限于一个直接前驱和一个直接后继的关系 树也只能有一个直接前驱也就是父节点 当我们需要表示多对多的关系时, 这里我们就用到了图. 图的举例 ...

  4. 【 MATLAB 】离散傅里叶级数(DFS)及 IDFS 的 MATLAB 实现

    有关离散傅里叶级数(DFS)我之前也写过一些博文,例如:离散周期信号的傅里叶级数(DFS) 这里我再次给出标准公式. 分析式: 其中: 综合式: 这里我必须先声明,关于分析式和综合式前面那个系数1/N ...

  5. 部署分布式文件系统(DFS)

    部署分布式文件系统(DFS) 使用 DFS 命名空间,可以将位于不同服务器上的共享文件夹组合到一个或多个逻辑结构的命名空间.每个命名空间作为具有一系列子文件夹的单个共享文件夹显示给用户.但是,命名空间 ...

  6. Java实现算法导论中图的广度优先搜索(BFS)和深度优先搜索(DFS)

    对算法导论中图的广度优先搜索(BFS)和深度优先搜索(DFS)用Java实现其中的伪代码算法,案例也采用算法导论中的图. import java.util.ArrayList; import java ...

  7. 广度优先搜索(BFS)与深度优先搜索(DFS)

    一.广度优先搜索(BFS) 1.二叉树代码 # 实现一个二叉树 class TreeNode:def __init__(self, x):self.val = xself.left = Nonesel ...

  8. 7.9模拟赛T1图的遍历(dfs)

    图的遍历(dfs) [题目描述] 对于一个有向图G来说,我们存在一个经典的遍历算法,就是DFS (深度优先搜索遍历).将G以1号点为起点进行DFS后,我们可以 得到G的一棵DFS遍历树T.就此,我们可 ...

  9. 7.6 T1 深度优先搜索(dfs)

    深度优先搜索(dfs) [题目描述] sol:50pts随便写写,就是大众分了,直接n2dpOK,100分要找点规律,需要数学头脑 官方题解 //#include <bits/stdc++.h& ...

最新文章

  1. Java中Runnable和Thread的区别
  2. Java面试题之类的静态代码块和静态属性等的加载顺序
  3. C++经典面试题(最全,面中率最高)
  4. linux下产生core文件以及不产生core文件的条件
  5. 细细讲述Java技术开发的那些不为人知的规则
  6. C# 中使用面向切面编程(AOP)中实践代码整洁
  7. 跳转,location.href,window.open(),load加载页面,iframe加载页面,兼容相关
  8. java jlist删除选中的项_java jlist removeListSelectionListener 怎样删除已经建好的 ListSelectionListener...
  9. 大数据_Hbase_面试题0001
  10. leetcode力扣23.括号生成
  11. Linux——基础知识及命令
  12. php h5 调用摄像头_怎样使用H5调用摄像头
  13. 计算机中算术逻辑单元负责,算术逻辑单元可实现 算术逻辑单元的发展
  14. hivesql 列转行,并用逗号分隔
  15. Angular 简介
  16. 某项目的双代号网络图如下所示_某工程项目的双代号网络计划如下图所示(时间单位:月)。...
  17. JZOJ 6310.glo【LIS】【线段树】
  18. 彻底消除电脑中的流氓软件与广告弹窗
  19. 浙江省2级C语言等级考试答案,2021年秋浙江省计算机等级考试二级C试卷及答案...
  20. Pytest如何重复执行N次脚本

热门文章

  1. mac使用被动ftp模式(pasv)_ftp主动模式和被动模式
  2. linux内核 mpls,将MPLS编译进linux内核中
  3. .net excel循环插数据_Python实战: 如何将数据从一个Excel文件移动到另一个?
  4. imagecomposition工程分析
  5. VC++编程实现多显示器控制(复制、横屏、纵屏,显示器个数)
  6. Android开发之shape画圆环的方法
  7. delimited mysql_在MySQL中存儲逗號分隔的數據
  8. java setrequestheader_Java SampleResult.setRequestHeaders方法代码示例
  9. MailCore2 SDK API
  10. php swoole 项目实战,Laravel 中使用 swoole 项目实战开发案例一 (建立 swoole 和前端通信)...