[UCSD白板题] Primitive Calculator
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的更多相关文章
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- [UCSD白板题] Maximize the Value of an Arithmetic Expression
Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- [UCSD白板题] Take as Much Gold as Possible
Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...
- [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 ...
- [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 ...
- [UCSD白板题] Sorting: 3-Way Partition
Problem Introduction The goal in this problem is to redesign a given implementation of the randomize ...
- [UCSD白板题] Majority Element
Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...
- [UCSD白板题] Binary Search
Problem Introduction In this problem, you will implemented the binary search algorithm that allows s ...
随机推荐
- MonoDev 冷门而好用的功能
[MonoDev 冷门而好用的功能] 1.Ctrl + B.跳转到对应的{}.(). 2.View -> Editor Layout -> 2 Columns.双屏工作. 3.Editr ...
- DOS命令(可查看本机IP地址各个网卡号)
网卡号指的是网卡的编号,也就是网卡的物理地址.查看方法:(以win7为例)打开开始 输入cmd 回车后进入dos命令行模式,然后输入ipconfig /all 回车查看结果找到"无线局域网适 ...
- nginx实现动静分离
环境信息如下: 测试目的: 访问jpg文件的时候,会请求到192.168.1.106上面 访问其他文件会请求到192.168.1.103上面 nginx的安装详见前面的文章 nginx的配置如下 wo ...
- Xml与DataTable相互转换方法
1.Xml与DataTable相互转换方法:http://www.cnblogs.com/lilin/archive/2010/04/18/1714927.html
- C#多线程实现方法
C#中实现线程应用开发的类都包含在了System.Threading命名空间中,比较常用的是Thread,ThreadPool类 Thread类构造函数作用是创建线程,有两个重载版本,一个带参数,一个 ...
- hibernate报错Unknown integral data type for ids : java.lang.String
package com.model; // Generated 2016-10-27 14:02:17 by Hibernate Tools 4.3.1.Final /** * CmDept gene ...
- C# NPOI 导入与导出Excel文档 兼容xlsx, xls
之前写了个小程序,导出一些数据成Excel,程序使用的是Microsoft.Office.Interop.Excel类来操作Excel. 在本机测试的时候都好好的,但是将生成文件放到其他电脑上却怎样也 ...
- How to build the Robotics Library from source code on Windows
The Robotics Library is an open source C++ library for robot kinematics, motion planning and control ...
- SSM框架——使用MyBatis Generator自动创建代码
版权声明:本文为博主原创文章,未经博主允许不得转载. 这两天需要用到MyBatis的代码自动生成的功能,由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是 ...
- 【redis】redis 在 windows 下安装使用
1.安装 windows 下 redis 可用客户端 : http://redisdesktop.com/download ; 或在 github 上下载 redis zip 包,解压包到某个目录并在 ...