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

, writes it down on the board, and then performs with it

operations of the two kinds:

  • divide the number

by ( must be divisible by

  • );
  • multiply the number

by

 

  • .

After each operation, Polycarp writes down the result on the board and replaces

by the result. So there will be

numbers on the board after all.

You are given a sequence of length

— 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

() — the number of the elements in the sequence. The second line of the input contains integer numbers (

) — rearranged (reordered) sequence that Polycarp can wrote down on the board.

Output

Print

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:

. It can match possible Polycarp’s game which started with .

题目大意:对给出的数字有两种操作,一是除以三,二是乘以二;
要求输出的数列能够从第一个开始经过操作变成下一个数字;
解法1:思维
因为3这个因数是要被除的,因此在整个序列中,3的幂必定是逐渐减少的。因此我们可以先统计所有数的3的幂,然后根据3的幂进行sort排序。而对于3的个数相同的时候,此时意味着不能进行除以3的操作,即只能进行*2的操作,因此,我们只需要将大的数排在后面即可。
代码:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long int h_3(long long int a)//3的个数
{
    long long int cnt=0;
    while(a%3==0)
    {
        cnt++;
        a=a/3;
    }
    return cnt;
}
bool cmp(long long int a,long long int b)
{
    long long aa,bb;
    aa=h_3(a);
    bb=h_3(b);
    if(aa==bb)
    {
        return a<b;
    }
    else return aa>bb;
}
int main()
{
    int n;
    long long int a[110];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n,cmp);
    for(int i=0;i<n;i++)
    {
        if(!i)cout<<a[i];
        else
        {
            cout<<" "<<a[i];
        }
    }
    cout<<endl;
    return 0;
}

解法2:暴力
第一个数字一定是既没有其他数能通过除以三得出又没有其他数可以通过乘以二得出的,这样判断出第一个数然后再往后一个一个推出即可
代码:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long int n,a[110],book[110];
    cin>>n;
    memset(book,0,sizeof(book));
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    long long int d,f;
    for(int i=0;i<n;i++)
    {
        int f=0;
        for(int j=0;j<n;j++)
        {
            if(j==i)continue;
            if(a[j]*2==a[i])
            {
                f=1;
                break;
            }
            if(a[j]%3==0)
            {
                if(a[j]/3==a[i])
                {
                    f=1;
                    break;
                }
            }
        }
        if(f==0)
        {
            d=i;
            break;
        }
    }
    cout<<a[d];
    book[d]=1;
    for(int i=1;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(book[j])continue;
            if(a[d]*2==a[j]||(a[d]%3==0&&a[d]/3==a[j]))
            {
                book[j]=1;
                d=j;
                cout<<" "<<a[j];
                break;
            }
        }
    }
    cout<<endl;
    return 0;
}

解法3:dfs
依次让每一个数作为起点用dfs尝试能否组成符合条件的数列
代码来自zyl

#include<bits/stdc++.h>
using namespace std;
long long int a[105],vis[105],f[105],flag=0,top,n;
int j(long long int x,long long int y)
{
    if(x*2==y)return 1;
    else if(x%3==0&&x/3==y)return 1;
    else return 0;
}
void dfs(int x)
{
    if(top==n-1&&!flag)
    {
        flag=1;
        for(int i=0;i<=n-1;i++)
        {
            if(i==n-1)cout<<f[i]<<endl;
            else cout<<f[i]<<" ";
        }
        return;
    }
    for(int i=1;i<=n;i++)
    {
        if(!vis[i]&&j(a[x],a[i])==1)
        {
            top++;
            f[top]=a[i];
            vis[i]=1;
            dfs(i);
            top--;
            vis[i]=0;
        }
    }
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=1;i<=n;i++)
    {
        memset(vis,0,sizeof(vis));
        vis[i]=1;
        top=-1;
        top++;
        f[top]=a[i];
        dfs(i);
        if(flag)break;
    }
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注