[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 ...
随机推荐
- Sharif University CTF 2016 - Smooth As Silk
Category: Crypto Points: 200 Solves: 11 Description: p > q n = p*q = 1146153281852525177586999436 ...
- 用"时:分:秒"的方式显示运行时间
import datetime,time start = datetime.datetime.now()...dosomething() end = datetime.datetime.now()pr ...
- iOS架构基础
1.概念:架构其实是一个设计上的东西,它可以小到类类之间的交互,可以大到不痛的模块之间,可以说不痛的业务部门之间的交互都可以从架构的层面去理解它. 2.解耦 方法一:代理 方法二:通知
- 【接口测试】jmeter的使用
学习地址: https://ke.qq.com/webcourse/index.html#course_id=89002&term_id=100088372&taid=15345918 ...
- JSON 的应用
使用 JSON 需要的 jar 包:
- Convert.ToInt32,int.Parse,int.TryParse,(int)的区别
1 (int)变量名[强制类型转换] 该转换方式主要用于数字类型转换,从int类型到long,float,double,decimal类型,可以使用隐式转换,但是从long类型到int类型就需要使用显 ...
- ArcEngine编辑保存错误:Unable to create logfile system tables
通过ArcEngine对多个SDE中多个图层进行批量编辑处理,其中有部分图层在结束编辑的时候出现错误提示(部分图层可以,只有两个数据较多的图层保存失败). 错误信息:Unable to create ...
- object.bind()方法的低版本兼容
Function.prototype.bind= function(obj){ var _self = this, args = arguments; return function() { _sel ...
- Valid Parentheses 使用递归的解法
class Solution { public: bool isValid(string s) { ||s.size()%!=)return false; unordered_map<char, ...
- net TreeView 递归
1.显示效果 2.数据insert脚本 insert into CITY(id,text,pid) values('1','城市',null)insert into CITY(id,text,pid ...