Problem Introduction

You are given a primitive calculator that can perform the following three operations with the current number \(x\): multiply \(x\) by 2, multiply \(x\) by 3, or add 1 to \(x\). Your goal is given a positive integer \(n\), find the minimum number of operations needed to obtain the number \(n\) starting from the number 1.

Problem Description

Task.Given an integer \(n\), compute the minimum number of operations needed to obtain the number \(n\) starting from the number 1.

Input Format.The input consists of a single integer \(1 \leq n \leq 10^6\).

Output Format.In the first line, output the minimum number \(k\) of operations needed to get \(n\) from 1. In the second line output a sequence of intermediate numbers. That is, the second line should contain positive integers \(a_0, a_2, \cdots, a_{k-1}\) such that \(a_0=1, a_{k-1}=n\) and for all \(0 \leq i < k-1\), \(a_{i+1}\) is equal to either \(a_i+1, 2a_i\), or \(3a_i\). If there are many such sequences, output any one of them.

Sample 1.
Input:

1

Output:

0
1

Sample 2.
Input:

5

Output:

3
1 2 4 5

Sample 3.
Input:

96234

Output:

14
1 3 9 10 11 22 66 198 594 1782 5346 16038 16039 32078 96234

Solution

[UCSD白板题] Primitive Calculator的更多相关文章

  1. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  2. [UCSD白板题] Maximize the Value of an Arithmetic Expression

    Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...

  3. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  4. [UCSD白板题] Take as Much Gold as Possible

    Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...

  5. [UCSD白板题] Points and Segments

    Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...

  6. [UCSD白板题] Number of Inversions

    Problem Introduction An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 ...

  7. [UCSD白板题] Sorting: 3-Way Partition

    Problem Introduction The goal in this problem is to redesign a given implementation of the randomize ...

  8. [UCSD白板题] Majority Element

    Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...

  9. [UCSD白板题] Binary Search

    Problem Introduction In this problem, you will implemented the binary search algorithm that allows s ...

随机推荐

  1. CC1310电源管脚

    对于48pin脚的CC1310而言,属于电源类的管脚如下: 上述电源类管脚的关系如下: 1 VDDS类管脚 VDDS类管脚包括VDDS.VDDS2.VDDS3和VDDS_DCDC四个管脚.其中VDDS ...

  2. Ubuntu:我不小心把/var/lock文件夹给删了

    在一个风和日丽的下午,不正常关闭minicom导致了device 没有正常解锁,于是使用minicom的时候提示 device is locked: 根据网上看到的方法只要把/var/lock 里面的 ...

  3. TextClock的基本使用

    0.介绍 TextClock是在Android 4.2(API 17)后推出的用来替代DigitalClock的一个控件!TextClock可以以字符串格式显示当前的日期和时间,因此推荐在Androi ...

  4. Ubuntu 查询 so 归属的 package

    . . . . . 今天 LZ 在运行一个程序的时候,出现找不到 so 库的情况: >$ ./core ./core: error : cannot open shared object fil ...

  5. Linux入门学习,开启端口号

    之前买了一个阿里云Centos 7服务器,默认开启22端口和80端口,想开启其他端口号如何做 1.开启端口号 [root@localhost ~]# /sbin/iptables -I INPUT - ...

  6. redis——持久化篇

    众所周知,redis是内存数据库,它把数据存储在内存中,这样在加快读取速度的同时也对数据安全性产生了新的问题,即当redis所在服务器发生宕机后,redis数据库里的所有数据将会全部丢失. 为了解决这 ...

  7. JDBC 工具类

    在Java编程过程中 我们需要连接数据库 所以数据库的连接和增删改查操作是非常基本的操作同时又非常的重要.同时在JDBC编程中 ,有很多的方法如更新个查找操作,我们可以对其进行封装,提高我们编码的效率 ...

  8. 反射,System.Type类

    http://m.blog.csdn.net/blog/woddle/40623333 两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏的生理情况.这是如何做到 ...

  9. 数据库事务中的隔离级别和锁+spring Transactional注解

    数据库事务中的隔离级别和锁 数据库事务在后端开发中占非常重要的地位,如何确保数据读取的正确性.安全性也是我们需要研究的问题.ACID首先总结一下数据库事务正确执行的四个要素(ACID): 原子性(At ...

  10. JS验证字符长度

    function getStrLength(str) { var cArr = str.match(/[^\x00-\xff]/ig); return str.length + (cArr == nu ...