codeforces 977D Divide by three, multiply by two

保姆级教程:在Ubuntu 22.04上搞定Isaac Sim Docker版(含端口与缓存配置避坑) 本文提供在Ubuntu 22.04上部署Isaac Sim Docker版的保姆级教程,涵盖环境准备、Docker容器部署、网络端口配置及性能优化等关键步骤。特别针对机器人与仿真器应用场景,详细解析避坑技巧和缓存管理策略,帮助开发者高效实现工业级机器人仿真解决方案。 阅读详情

题目

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 .

分析

【题意】
有一个原始串a[n],每一位都满足下列的其中一个条件

  • a[i+1]=a[i]∗2
  • a[i+1]=a[i]/3 (a[i]必须整除3)

现在用打乱的顺序给出该串,输出原串。

【思路】
每个点有两种转移情况,而且转移不可逆,可以放到有向图上思考:给一个含有n个点,最多2*n条边的有向图,找出一条经过所有点的路径。暴力搜索一遍表面上看似有2n种情况,但其实对于每个点的搜索,它最多就把n个点访问一遍,也就是n次,所以对n个点全部搜索一遍总共不会超过n2次。

【注意】
搜索结束的控制:只有把所有的点不重复的遍历一次后,才算成功:保证遍历不重复的同时,在搜索中增加一维来记录已经遍历过的个数;
边的存在不是明显的,需要检查下一次要搜索的数是否存在,用二分检索以降低复杂度。

代码

#include<stdio.h>
#include<vector>
using std::vector;

#define N_max 105
typedef unsigned long long ll;
int g[N_max][N_max];
ll ipt[N_max];
int n;
vector<int>vis;
#define debug no_bug_at_all
ll ans[N_max];
int dfs(ll cur,int tot) {
    if (tot > n)return 1;
    //二分检查cur是否存在
    int l = -1, r = n,m;
    while (l+1 < r) {
        m = (l + r) / 2;
        if (ipt[m] <= cur)l = m;
        else r = m;
    }
    //不存在
    if (l == -1)return 0;  
    if(ipt[l] != cur)return 0;
    //已经访问过
    if (vis[l] == 1)return 0;
    //标记访问位置
    vis[l] = 1;
    if (cur % 3 == 0)
        if (1 == dfs(cur / 3, tot+1))
        {
            ans[tot] = cur;
            return 1;
        }
    if (1 == dfs(cur * 2, tot+1)) {
        ans[tot] = cur;
            return 1;
    }
    else { 
        vis[l] = 0;
        return 0; 
    }
}
int main() {

    scanf("%d", &n);
    for (int i = 0; i < n; ++i) 
        scanf("%llu", &ipt[i]);
    //排序以二分查找
    sort(ipt, ipt + n);
    for (int i = 0; i < n; ++i) {
         vis.resize(n);
         if (1 == dfs(ipt[i], 1))
         {
             for (int i = 1; i <= n; ++i) {
                 printf("%llu%c", ans[i], i == n? '\n' : ' ');
             }
             return 0;
         }
    }
}

总结

其实题目中的数字一定是非重复的,使用vis数组不仅没有意义,而且另一方面,假如有重复的,vis数组将会带来错误。考虑对重复数字的改进,可以对输入进行预处理,把重复的数字的个数记录到cnt数组,同时保证剩下的数没有重复;另一方面,搜索时要判断是否还有可用的数可以用(cnt[x]>0),每遍历到这个数字,就让cnt[x]–。

【AI气象6】Skilful nowcasting of extreme precipitation with NowcastNet 极端降水是气象灾害的重要因素,因此迫切需要通过高分辨率、长时间预报和局部细节的精准短期预报来减轻其社会经济影响。当前的方法存在模糊、衰减、强度或位置误差,基于物理的数值方法在捕捉关键的混沌动态(如对流起始)时面临挑战,而数据驱动的学习方法则未能遵循内在的物理法则(如平流守恒)。我们提出了 NowcastNet,这是一种针对极端降水的非线性短期预报模型,将物理演变方案与条件学习方法统一到一个端到端的神经网络框架中,通过优化预测误差进行训练。基于来自美国和中国的雷达观测数据,我们的模型能够生成物理上合理的降水短 阅读详情

相关推荐

ROS2实战:构建模块化启动文件(launch file)以驱动复杂机器人系统

本文详细介绍了ROS2中构建模块化启动文件(launch file)的最佳实践,帮助开发者高效管理复杂机器人系统的节点启动。通过功能分解、参数化设计和嵌套启动等技术,实现批量管理、依赖控制和配置统一,显著提升系统稳定性和调试效率。文章包含实战代码示例和工程化建议,特别适合自动驾驶、物流机器人等场景应用。

weixin_28744613的博客 214

Divide by three, multiply by two ( 深搜 )

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

学习笔记 2229

在昇腾910B服务上部署搭建适配PDF解析工具Mineru2.5开源项目

本文介绍了在昇腾910B服务器上部署开源PDF解析工具Mineru2.5的过程。首先从昇腾社区下载mindie镜像并运行容器,安装系统依赖。针对部署过程中遇到的多个报错问题(如torchvision与torch版本不匹配、昇腾软件栈与NumPy 2.0不兼容等),提供了具体解决方案和兼容版本。通过设置环境变量、安装指定版本的Python依赖包,成功部署Mineru项目并下载模型。最后运行测试,验证了在NPU设备上运行pipeline的可行性。整个过程详细记录了版本适配和错误处理方法,为类似部署场景提供了参考

一起加油吧 2163

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

金属粘结剂喷射3D打印技术:工艺流程与后处理关键

金属粘结剂喷射(Binder Jetting)是一种创新的增材制造技术,通过逐层喷射粘合剂将金属粉末粘结成型,再经过脱脂和烧结等后处理工序获得最终零件。这项技术的核心优势在于无需支撑结构、可实现批量生产,并且材料利用率高。在3D打印领域,金属BJ技术特别适合中小尺寸、结构复杂且表面质量要求高的零件生产。其工艺流程包括前期数据处理、打印生胚阶段、拆包与清粉,以及关键的渗透、脱脂和烧结后处理环节。后处理工艺的控制直接决定了零件的机械性能和尺寸精度,其中烧结工艺尤为关键,需要精确控制温度曲线和气氛环境。金属BJ打

weixin_35751194的博客 83

【正点原子I.MX6U-MINI移植篇】Ubuntu-base根文件系统移植构建过程详解(四)

能不能在ARM板上运行Ubuntu呢?答案肯定是可以的,Ubuntu是Linux系统的一种,可以简单的将Ubuntu理解为一个根文件系统,和我们用busybox、buildroot制作的根文件系统一样。因此移植Ubuntu也就是将Ubuntu根文件系统移植到我们的开发板上。

果果小师弟的博客 3767

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

五段式S型算法笔记

又因为有 v-v0= 0.5jt通式: 所以有 vm-v0= 0.5jtm 所以有4vm-4v0= 2jtm。算法1: v=2vm-v0-0.5*jt+2jt*tm-2jtm*tm。加加速度j 自己设置 已知,vmax自己设置已知;所以有4vm-4v0= 2jtm带入算法1。

qq_36658033的博客 681

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

EBS开发_离散工单创建API

--以下代码于20171213在TONY环境中实现 DECLARE     l_iface_rec    wip.wip_job_schedule_interface%ROWTYPE; --工单任务接口表     l_group_id     NUMBER;     l_returnstatus VARCHAR2(40);     l_errormsg     VARCHAR2(2000

蜗牛先生快跑的博客 2596

【复现论文】Looking here or there? Gaze Following in 360-Degree Images

论文:Looking here or there?

RichardoMu的博客 1128

Codeforces 977D. Divide by three, multiply by two

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

鸿杰 的博客 408

YOLOv10 的损失函数解析

YOLOv10 在 YOLO 系列的演进中进一步优化了任务解耦与动态平衡策略,通过 ​多模态损失融合、自适应样本分配 和 ​轻量化设计,显著提升了检测精度与推理效率。

浩瀚之水的专栏 1378

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

152

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

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

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

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

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

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

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

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

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

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

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 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+1​,保证这些数有解,输出排序后的数 AC代码 /*************************************************************************...

执念 212
上一篇: codeforces 977E Cyclic Components
下一篇: codeforces 977C Less or Equal
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值