[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 ...
随机推荐
- 何为babel / gulp
Babel主要用来将新版本的javascript(ES6,ES7)编译为ES5,目前它对于新标准的支持程度甚至高于Chrome浏览器.通过引入预设babel-preset-react,babel还能解 ...
- jdk环境变量的配置并检测是否配置成功
JDK环境变量配置进行java开发,首先安装JDK,安装后进行环境变量配置1,下载JDK(http://java.sun.com/javase/downloads/index.jsp)2.安装jdk- ...
- 通过rpc访问比特币核心钱包
开发环境和工具 1. window 10 64 2. 比特核心钱包:bitcoin core 64 配置过程 1. 下载比特币核心钱包,下载链接https://bitcoin.org/en/downl ...
- UNICODE 7.0定义的表情符
td.graph { font-size:24px; } UNICODE 7.0定义了78个表情符,从0x1F600到0x1F64F(其中0x1F643和0x1F644没有定义).下表中列出了这些表情 ...
- 检测是否IE浏览器
function browserIsIE(){ if (window.ActiveXObject) return true; else{ var u_agent = navigator.userAge ...
- ie,火狐,谷歌 select清除默认样式 设置新的样式
select { border: solid 1px #000; /*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/ appearance:none; -moz-appearan ...
- jquery获取所选元素的outerHTML
大家都知道原生js可以获取匹配元素的内部html和外部html,内部是innerHTML,外部是outerHTML,原生js的dom对象是存在这两个属性的. 如果用jQuery如何获取匹配元素(包括自 ...
- Oracle学习线路
出自huyangg的博客,地址是:oracle学习路线图 1.sql.pl/sql(网上有很多的视频,可以做一个简单的入手,然后看几本书,多做实验) 作为oracle的基本功,需要大家对sql和 ...
- DFTX 笔试
char aa[8] = "abcd"; printf("%d",strlen(aa)); 4 printf("%d",sizeof(aa ...
- easy ui 框架
Easy UI 准备工作(搭建) 1.在WebRoot 的目录下创建js 文件夹,在文件夹中倒入一下两个包 Jquery.easyui.min.js jquery.min.js 2.在WebRoot ...