[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 ...
随机推荐
- mave的变量说明
Maven提供了三个隐式的变量可以用来访问环境变量,POM信息,和Maven Settings env env变量,暴露了你操作系统或者shell的环境变量.便 如在Maven POM中一个对${en ...
- linux系统数据盘挂载教程
将数据盘挂载为/www命令:#mkdir /www & mount /dev/sdb1 /www ----------------------------------------------- ...
- 有关STL 标准模板库
1.vector 本质:对数组的封装 特点:读取能在常数时间内完成
- 后台dom拼接xml,动态显示统计图
这个东西让我好生头疼,贴代码吧 // 两秒后模拟点击 setTimeout(function() { // IE if(document.all) { document.getElementById( ...
- android中string.xml中%1$s、%1$d等的用法
今天在研究前辈写的代码的时候,突然发现string里面出现了<stringname="item_recent_photo">最近拍摄%1$s</string> ...
- Vue.js 组件重要选项
(一) new一个Vue对象的时候,可以设置它的属性,其中最重要是三个,分别是 data,,methods,watch. new Vue({ data:{ a:, b:[] }, methods:{ ...
- JsonString,字典,模型之间相互转换
NSData转字符串 [NSString alloc] initWithData: encoding:] 模型转字典 attInfo.keyValues 字典转模型 ZTEOutputInfo *ou ...
- sap MD04中常用函数
1. 需求溯源 : MD_PEGGING_NODIALOG 2. 实时库存 : MD_STOCK_REQUIREMENTS_LIST_API 这个函数中MDPSX 和 MDEZX 是通过 MDPS 的 ...
- [刘阳Java]_斗胆介绍一下Eclipse快捷键大全[超详细]_第6讲
斗胆让我在这里介绍一下Eclipse快捷键有哪些 ctrl+shirt+r 打开资源 这组快捷键可以让你开打Eclipse工作区中任何一个文件,你只需要输入你想查找的文件名字即可,而且绝对支持模糊检索 ...
- android端,webview内url跳转到app本地
这是和一个前端同事沟通. app内嵌入他的web页,要通过web页内的url跳转到app的详细内容. 他的android同事,没有思路. 其实嵌入web页,用的webview控件,只要能找到webvi ...