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. [原创.数据可视化系列之三]使用Ol3加载大量点数据

    不管是百度地图还是高德地图,都很难得见到在地图上加载大量点要素,比如同屏1000的,因为这样客户端性能会很低,尤其是IE系列的浏览器,简直是卡的要死.但有的时候,还真的需要,比如,我要加载全球的AQI ...

  2. [python] python实现2048游戏,及代码解析。

    我初学python,有不对之处望大家指教.转载请征得同意. 我在网络上也找了一些2048游戏代码的讲解,但都不是特别详细.所以我希望能够尽量详细的讲解.同时,有的地方我也不懂,希望大家能帮助补充.我会 ...

  3. quartz学习

    quartz是一个作业调度框架,用于指定工作(作业)在指定时间执行——定时工作. quartz的核心接口有: Scheduler接口:Scheduler是job的执行对象,用于工作的执行. Job接口 ...

  4. 特殊字符(包含emoji)的梳理

    背景知识 emoji表情符号,是20世纪90年代由NTT Docomo栗田穣崇(Shigetaka Kurit)创建的,词义来自日语(えもじ,e-moji,moji在日语中的含义是字符).emoji可 ...

  5. 网络编程——URL编程

    URL:是统一资源定位器的简称,它表示Internet某一资源的地址.通过URL我们可以访问Internet上的各种网络资源,比如最常见的www,ftp站点.浏览器通过解析给定的URL可以在网络上查找 ...

  6. extern关键字

    1.extern "C" void func(){...} extern可以置于变量或者函数前,以标示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其 ...

  7. php常用的字符串函数

    addslashes -- 使用反斜线引用字符串 chr -- 返回相对应于 ascii 码值所指定的单个字符. chunk_split -- 将字符串分割成小块 count_chars --  返回 ...

  8. alphaBlend

    // Alpha = srcAlpha + dstAlpha - srcAlpha * dstAlpha / 0xFF;// R = (srcR * srcAlpha + dstR * dstAlph ...

  9. ElasticSearch 嵌套映射和过滤器及查询

    ElasticSearch - 嵌套映射和过滤器 Because nested objects are indexed as separate hidden documents, we can’t q ...

  10. Loaders

    Android3.0之后引入了加载器,支持轻松在Activity和Fragment中异步加载数据.加载器具有以下特点: 1.可用于任何Activity和Fragment 2.支持异步加载数据 3.监控 ...