Codeforces 977D. Divide by three, multiply by two

MOOS-ivp 实验三 MOOS简介(1) 系列文章目录 提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加 例如:第一章 Python 机器学习入门之pandas的使用 提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录系列文章目录前言一、pandas是什么?二、使用步骤1.引入库2.读入数据总结 前言 提示:这里可以添加本文要记录的大概内容: 例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。 提示:以下是本篇文章正文内容,下面案例 阅读详情

Divide by three, multiply by two

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:

divide the number x by 3 (x must be divisible by 3);
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

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.


题意:
给你一组数,这组数是由一个数经过除以三,乘以二变化来的,这个数也给了
让你输出变化的顺序
如给的是4 8 6 3 12 9
那么答案就是 9 3 6 12 4 8
结果一定是唯一的

这道题,脑袋不知道怎么了,周赛的时候做没有做出来,。,,
回头补了一下,发现不是很难
由于n比较小,
第一种方式我采用的是暴力建链表
对于任意两个数a b
如果 a * 3 == b || a = b * 2
那么就说明,在生成的序列中 a 在 b 的前面


//暴力建立链表 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;


ll a[128];
int l[128];
bool f[128];
int main()
{
	int n;
	cin>>n;
	memset(f,0,sizeof(f));
	memset(l,-1,sizeof(l));
	for(int i=0;i<n;i++)
		cin>>a[i];
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
		{
			if(i==j)	continue;
			if(a[i]*2==a[j]||a[i]==a[j]*3)	l[i]=j,f[j]=1;// a[i] 在前,a[j]在后 没有被用过的是头节点 
		}
	int t = -1;
	for(int i=0;i<n;i++)
		if(!f[i])
		{
			t = i;
			break;
		}
	for(t;~t;t=l[t])
		printf("%I64d%c",a[t]," \n"[t==-1]);
	return 0;
}

第二种方法是:
我们经过观察到结果序列规律:
能整除3的次数越多,优先级越高,次数相同的按照从小到大排列

于是我用pair<int,long long> 来存数,第一个存优先级,第二个存数
对pair进行sort的时候,会自动按照first从小到大排序,first相等时按照second从小到大排序


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 107;
ll a[MAXN];
ll f(ll x)
{
	ll ans = 0;
	while(x%3==0)
	{
		x/=3;
		ans++;
	}
	return ans;
}
int main()
{
	int n;
	cin>>n;
	vector<pair<int,ll> > v;
	v.resize(n);
	for(int i=0;i<n;i++)
	{
		scanf("%I64d",&v[i].second);
		v[i].first = -f(v[i].second); 
	}
	sort(v.begin(),v.end());//pair<> 数组的性质,先按first从小到大排序,first相等时按照second从小到大排序 
	for(int i=0;i<n;++i)
		printf("%I64d%c",v[i].second," \n"[i==n-1]); 
	return 0;
}

Zotero插件安装保姆级教程:从Jasminum到Sci-Hub,一站式搞定文献管理 本文提供Zotero插件安装的保姆级教程,从Jasminum到Sci-Hub,一站式搞定文献管理。通过详细配置和实战案例,帮助用户高效构建文献工作流,提升研究效率。重点介绍了元数据抓取、PDF管理、文献分析和知识产出等关键插件的使用方法。 阅读详情

相关推荐

Unreal5(虚幻五) 实现UI与C++代码逻辑的交互 保姆级教程

本人有Unity基础,刚学UE几天,把这一套处理流程记录一下备用。

qq_42915442的博客 9815

Codeforces977 D. Divide by three, multiply by two(思维、质因子)

题意: 给定长度为n的序列a 要求重新排列序列,使得序列满足a(i+1)=2*a(i)或a(i+1)=a(i)/3 即后一个数是前一个数的两倍或者三分之一 保证一定有解 数据范围:n<=100 解法: 1.看到dfs的解法 2.看到拓扑排序的解法(需要证明直接建图不存在环) 3.看到从质因子方向考虑的解法: 乘2可以看成添加一个质因子2,除3可以看成去掉一个质因子3 因为质因子3的个数决定了后面能有多少个除以3的,所以质因子3最多的数一定在最前面 如果有质因子3数量相同的数,因为乘2会变大,为了使得序

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 292

SAP 资产主数据屏幕增强:对比字段追加与子屏幕/页签 2 种方案优劣

本文深入对比了SAP资产主数据(AS01/AS03)屏幕增强的两种方案:字段追加与子屏幕/页签。从技术实现、业务适配、系统维护等维度分析各自优劣,提供方案选型决策树和混合方案实践建议,帮助企业在SAP实施中做出最优选择。

weixin_28559803的博客 284

Codeforces 977D 题解报告

一、题目 http://codeforces.com/contest/977/problem/D 二、分析 依题意,可以考虑一个数是3的多少次幂,可以有余数。 比如18是3的2次幂,余数为2; 9是3的2次幂,余数为0; 2是3的0次幂,余数为2。 首先要把幂从高到低的顺序排列,比如9(3的2次幂)必然排在3(3的1次幂)的前面。 对于同一次幂,要按从小到大的顺序排序,这样后面...

小树学信奥 524

OSGEarth 基于鼠标位置缩放

说明 osgearth 原有漫游器EarthManipulator 不支持此功能,所以需要修改此漫游器代码,其中主要原理就是在缩放的同时也要改变当前漫游器中心焦点的位置,从而保持鼠标指定点的位置缩放是不改变。 核心代码修改部分 EarthManipulator 的头文件zoom方法添加参数view /** * Zoom the camera using delt...

Rsoftwaretest的专栏 3782

【Python脚本】Python自动大麦网抢票,准点原价秒杀演唱会门票,拒绝黄牛交易!从我做起!!!1

你是否错过了周杰伦的票,最近又没抢到五月天的票,不要太难过,今晚有张学友的票,后天还有张杰的票,加油哦!

weixin_45841831的博客 6万+

Codeforces 977D(思维)

传送门题面:D. Divide by three, multiply by twotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp likes to play with numbers. He takes some integer...

Chen.Jr的博客 1056

如何正确的计算两个角度的角度差?

这一步是必要的,因为它确保了无论a相对于b是在逆时针方向还是顺时针方向,d2总是正确地反映出绕圆相反方向的角度差。这样,当函数进行最后的比较时,它能够准确地比较两个方向的旋转路径并选择最短的一个。

小秋 AI SLAM 入门实战教程 4137

Codeforces977D ---Divide by three, multiply by two 深搜+map存出现的数

传送门:点我 题意:给定n长度的序列,重排成后一个数是前一个数除以三,或者后一个数是前一个数乘二,要求输出这个序列。 思路:大力深搜,对每个数搜除3的和乘2的是否出现过,然后继续搜下去。如果有一个数搜能搜完整个序列,那就输出这个数开始的一整个序列。 代码: #include<bits/stdc++.h> using namespace std; typedef uns...

weixin_30480583的博客 158

YOLO26涨点改进 | 细节涨点篇 | 源自SCI一区 | YOLO26引入HWD小波下采样模块,而不会显著增加计算开销,(适合小目标分割任务、小目标检测任务)

本文提出了一种基于Haar小波变换的下采样模块HWD,用于改进YOLO26目标检测性能。HWD模块通过小波变换降低特征图分辨率,同时将空间信息编码到通道维度,有效保留了关键信息。相比传统下采样方法,HWD能显著减少信息不确定性,提升小目标检测精度。实验表明,该模块可无缝集成到CNN架构中,不增加计算开销。文章详细介绍了HWD的实现原理、代码结构以及在YOLO26中的集成方法,并提供了两种改进版yaml配置文件。该创新模块已在多种数据集上验证有效,特别适合语义分割和目标检测任务。

Ai缝合怪、助力小伙伴高效发论文 401

ARFoundation系列讲解 - 24 锚点

一、添加平面检测和平面可视化 请参考 ARFounnation快速入门-08平面检测 案例 二、添加射线检测 1.在AR Session Origin 组件上添加ARRaycastManager 脚本

a451319296的博客 4877

codeforces 977D Divide by three, multiply by two

D. Divide by three, multiply by twotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp likes to play with numbers. He takes some integer numbe...

shyoldman的博客 749

Stanford Dogs 犬类识别实战:基于 YOLOv8 的 120 类目标检测与图形界面实现

本项目通过YOLOv8实现了Stanford Dogs数据集的目标检测功能,完整包含了数据准备、模型训练、检测推理、图形界面四大模块。初学者使用YOLOv8n(nano版)加速训练,熟悉流程后可切换到YOLOv8s/m。图像上传与检测可进一步增强,例如显示识别框、标注标签。可以添加模型选择、检测阈值调整等功能,提升实用性。通过网盘分享的文件:基于yolov8的狗类品种识别系统链接: https://pan.baidu.com/s/1UYV4h2WnK1RxWWKoPrS-RA?

FJN110的博客 84

CodeForces - 977D:Divide by three, multiply by two

题目 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: divide the number x by 3 (x must be divisible by 3); multiply the number x by 2. After each op

Mr.RainsdRop的胡思乱想 351

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

CodeForces977D Divide by three, multiply by two

题目 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...

weixin_30878501的博客 101

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

codeforces 解题报告 977D. Divide by three, multiply by two DFS

http://codeforces.com/problemset/problem/977/D解题思路:1.给一个序列,要求给它重新排序,规则要满足以下其中之一条件:前一个数是后一个数的三倍后一个数是前一个数的两倍2.序列长度只有100,并且保证答案一定存在,直接裸DFS爆搜答案import java.util.*; import static java.lang.System.exit; pu...

三生锁链 265

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

Divide by three, multiply by two CodeForces - 977D(思维+map)

D. Divide by three, multiply by twotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp likes to play with numbers. He takes some integer numbe...

JiaYao的博客 549

Codeforces--977D--Divide by three, multiply by two

题目描述: 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: divide the number x by 3 (x must be d...

猩猩是个好程序员的博客 291

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, and then performs with it n−1n−1 operations of the two kinds:divide the number xx by 33 (xx must be d...

这是一个博客 252

【Codeforces - 977D】Divide by three, multiply by two(思维构造)

题干: Polycarp likes to play with numbers. He takes some integer numberxx, writes it down on the board, and then performs with itn−1n−1operations of the two kinds: divide the numberxxby33(xxmu...

xuanweiace的博客 402

CodeForces - 977D D. Divide by three, multiply by two dfs路径打印

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

pxlsdz的博客 600

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

Polycarp likes to play with numbers. He takes some integer numberxx, writes it down on the board, and then performs with itn−1n−1operations of the two kinds: divide the numberxxby33(xxmust ...

weixin_30415113的博客 210

codeforces 977D Divide by three, multiply by two(DFS)

一、原题地址传送门二、大致题意    给出n个数,有两种操作。1、a[i]这个数能被3整除成为a[i+1]。2、a[i]×2=a[i+1].三、思路 裸的dfs.四、代码#include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;cstdlib&gt; #include&lt;cstring&gt; #include&lt;cmath&g...

Amove 491

Divide by three, multiply by two CodeForces - 977D (拓扑排序)

Divide by three, multiply by two Polycarp likes to play with numbers. He takes some integer numberx, writes it down on the board, and then performs with itn−1operations of the two kinds: divide...

SEVENY_的博客 407

CodeForces 977 D. Divide by three, multiply by two 除以三排序

题意:给定一个序列,重排,使后面一个数是前面一个数的2倍或者(1/3);思路:法一:建图,跑dfs;法二:一共有两种关系,题说一定有解,所以左边的一定是右边的数的3倍,或者除去3的倍数的因子以外的因子右边是左边的两倍;这样的话对于每个数除以三并记录个数,然后排序;法二代码:#include &lt;bits/stdc++.h&gt; using namespace std; typedef lon...

床前明月光 333

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, and then performs with it n−1n−1operations of the two kinds:divide the number xx by 33 (xx must be di...

qq_38586027的博客 334
上一篇: Codeforces 997B. Two-gram——————水题复习一下map
下一篇: Codefoces 977E - Cyclic Components——————dfs
陶鸿杰
博客等级 码龄9年 93粉丝 298原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值