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:

1.divide the number x by 3 (x must be divisible by 3);
    2.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≤n≤100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a1,a2,…,an (1≤ai≤3⋅1018) — 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
Copy

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个数,让你将这些数排序,后一个数应该是前一个数的二倍或 三分之一(必须能被三整除),输出任意一种结果,数据保证有解。

解题思路:使用DFS深搜,n个点,如果点j的权重是点i的二倍或三分之一, 那么点 i  到 点 j 有一条单向边 。最终的排序结果,也就是遍历所有点的任意一种方式。

 #include<cstdio>
#include<map>
#include<algorithm>
#define ll long long int
using namespace std;
int n,k,flag;
int counts;
map<ll,int>mp;
ll b[];
void DFS(ll x)
{
if(counts==n-)
{
flag=;
return ;
}
if(x%==&&mp[x/]==)
{
counts++;
b[counts]=x/;
DFS(x/);
}
if(mp[x*]==)
{
counts++;
b[counts]=x*;
DFS(x*);
}
return ;
}
int main()
{
int i;
ll a[];
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%lld",&a[i]);
mp[a[i]]=;
}
flag=;
for(i=;i<n;i++)
{
counts=;
b[]=a[i];
DFS(a[i]);
if(flag)
{
for(i=;i<n;i++)
{
if(i==n-)
{
printf("%lld\n",b[i]);
}
else
{
printf("%lld ",b[i]);
}
}
break;
}
}
return ;
}

对于这一道题其实还有另外一种方法,自定义sort的排序方式我们试分析。对于这个序列,因为3这个因数是要被除的,因此在整个序列中,3的个数必定是逐渐减少的。因此我们可以先统计所有数的3的个数,然后根据3的个数进行sort排序。而对于3的个数相同的时候,此时意味着不能进行除以3的操作,即只能进行*2的操作,因此,我们只需要将大的数排在后面即可。

 #include<stdio.h>
#include<string.h>
#include<algorithm>
#define LL long long int
using namespace std;
int my_cmp(LL a,LL b)
{
int count_a,count_b;
count_a=;
count_b=;
while(a%==)
{
a=a/;
count_a++;
}
while(b%==)
{
b=b/;
count_b++;
}
if(count_a==count_b)
{
if(a<b)
{
return ;
}
else
{
return ;
}
}
else if(count_a>count_b)
{
return ;
}
else if(count_a<count_b)
{
return ;
}
}
int main()
{
LL a[];
int i,n;
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%lld",&a[i]);
}
sort(a,a+n,my_cmp);
for(i=;i<n;i++)
{
printf("%lld%c",a[i],i==n-?'\n':' ');
}
return ;
}

Divide by three, multiply by two(DFS+思维)的更多相关文章

  1. 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,遍历这个序列 ...

  2. hdu6035[dfs+思维] 2017多校1

    /*hdu6035[dfs+思维] 2017多校1*/ //合并色块, 妙啊妙啊 #include<bits/stdc++.h> using namespace std; ; const ...

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

  4. D. Eternal Victory(dfs + 思维)

    D. Eternal Victory time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Divide by three, multiply by two CodeForces - 977D (思维排序)

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

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

  7. HDU 6060 RXD and dividing(dfs 思维)

    RXD and dividing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  8. hiboCoder 1041 国庆出游 dfs+思维

    先抽象出一棵以1做为根结点的树.给定了访问序列a[1..m]. 考虑两种特殊情况: 1.访问了某个a[j],但是存在a[i]没有访问且i < j,出现这种情况说明a[j]一定是a[i]的祖先节点 ...

  9. newcoder F石头剪刀布(DFS + 思维)题解

    题意:wzms 今年举办了一场剪刀石头布大赛,bleaves 被选为负责人. 比赛共有 2n 个人参加, 分为 n 轮, 在每轮中,第 1 位选手和第 2 位选手对战,胜者作为新的第 1 位选手, 第 ...

随机推荐

  1. OO 第三次博客总结

    调研规格化设计 1950年代,第一次分离,主程序和子程序的分离程序结构模型是树状模型,子程序可先于主程序编写.通过使用库函数来简化编程,实现最初的代码重用.产生基本的软件开发过程:分析—设计—编码—测 ...

  2. JS如何截取-后面的字符串

    str为要截取的字符串  通过获取字符串中“-”的坐标index,其他特殊字符以此类推 var index=str.lastIndexOf("\-"); str=str.subst ...

  3. C++的前置++、后置++和前置--、后置--

    一.C++的前置++和后置++ 在C++中,运算符重载是你必须要掌握的重点,而前置++和后置++有什么区别呢?其实前置++和后置++是有关于 影响效率的问题,前置++比后置++的效率要高,原因是因为前 ...

  4. Python 整数 长整数 浮点数 字符串 列表 元组 字典的各种方法

    对于Python, 一切事物都是对象,对象基于类创建!! 注:查看对象相关成员var,type, dir 一.整数 如: 18.73.84 每一个整数都具备如下需要知道的功能: def bit_len ...

  5. A1033

    找出最小开销. 思路: 出发点的加油站编号设为0,终点的加油站编号设为n,其他加油站编号按距离依次排序. 如果0号加油站的距离!=0,则无法出发,行驶距离为0. 从起点开始,寻找规则为,如果存在油价小 ...

  6. Go语言的包管理

    1 概述 Go 语言的源码复用建立在包(package)基础之上.包通过 package, import, GOPATH 操作完成. 2 main包 Go 语言的入口 main() 函数所在的包(pa ...

  7. C#基础 base与this关键字

    base和this在C#中被归于访问关键字,顾名思义,就是用于实现继承机制的访问操作来满足对对象成员的访问,从而为多态机制提供更加灵活的处理方式. this是指当前对象本身,而base则是在继承类中访 ...

  8. PHP base64转换成图片

    获取base64文件 $image="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASIAAAEiCAYAAABdvt+2AAAgAElEQV ...

  9. 20155236 2016-2017-2 《Java程序设计》第十周学习总结

    20155236 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 程序员所作的事情就是把数据发 ...

  10. 20155308 加分项——C语言实现Linux的pwd命令

    20155308 加分项--C语言实现Linux的pwd命令 实现要求 学习pwd命令 什么是pwd pwd' 代表的是'Print Working Directory'(打印当前目录).如它的名字那 ...