Divide by three, multiply by two(cf 977D)

Description

Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, and then performs with it n1n−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 (2n1002≤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 (1ai310181≤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.

Sample Input Output

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 

题解:给你一个被打乱的序列,每次操作,都要把这个数除以3或者乘2,使之变成下一个数,问你满足条件的原序列数。以下有三种ac方法。

代码一:

#include <iostream>
#include <vector>
using namespace std;
int n;
long long a[110];//输入
int b[110];//记录输出顺序
int c[110];//查找答案头
int main()
{
    long long n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(a[i]*3==a[j])//乘3与除3效果相同,只不过要把b,c两数组下标与赋值改变
            {
                b[j]=i;//记录其下一个输出
                c[i]=j;
            }
            else if(a[i]*2==a[j])
            {
                b[i]=j;
                c[j]=i;
            }
        }
    }
    int ss=-1;
    for(int i=1;i<=n;i++)//查找第一个输出
    {
        if(c[i]==0)
        {
            ss=i;
            break;
        }
    }
    while(ss!=0)
    {
        cout<<a[ss]<<" ";
        ss=b[ss];
    }
    return 0;
}

代码二:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define maxn 10001
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define eps 0.00000001
using namespace std;
typedef long long ll;
map<ll,int>mp;
ll a[111];
ll ans[111];
int cur,n;
bool DFS(ll x)
{
	if(cur==n-1)//如果找完,退出
	{
		ans[cur++]=x;
		return 1;
	}
	if(mp[x*2]==1)
	{
		ans[cur++]=x;
		if(DFS(x*2)==1)
			return 1;
		cur--;//不合适,该位置重新选择
	}
	if(x%3==0&&mp[x/3]==1)
	{
		ans[cur++]=x;
		if(DFS(x/3)==1)
			return 1;
		cur--;
	}
	return 0;
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
	{
		cin>>a[i];
		mp[a[i]]=1;
	}
	bool flag;
	for(int i=0;i<n;i++)
	{
		cur=flag=0;
		flag=DFS(a[i]);
		if(flag)
			break;
	}
	for(int i=0;i<n;i++)
	{
		if(i)
			printf(" ");
		printf("%lld",ans[i]);
	}
	printf("\n");
    return 0;
}

代码三:

 代码分析:对于这个序列,因为3这个因数是要被除的,因此在整个序列中,3的个数必定是逐渐减少的。因此我们可以先统计所有数的3的个数,然后根据3的个数进行sort排序。而对于3的个数相同的时候,此时意味着不能进行除以3的操作,即只能进行*2的操作,因此,我们只需要将大的数排在后面即可。某大神代码,暂时没看懂,附上地址:https://blog.csdn.net/weixin_39453270/article/details/80252167

#include <bits/stdc++.h>
#define maxn 105
using namespace std;
typedef long long ll;
int n;
ll a[maxn];
ll get_3(ll x)
{
    int cnt=0;
    while((x%3)==0) cnt++,x/=3;
    return cnt;
}
bool cmp(ll a,ll b)
{
    ll x=get_3(a),y=get_3(b);
    if(x==y) return a<b;
    return x>y;
}
int main()
{
    int n;
    cin>>n;
    for(int i=1; i<=n; i++) cin>>a[i];
    sort(a+1,a+1+n,cmp);
    for(int i=1; i<=n; i++)
    {
        cout<<a[i]<<" ";
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值