CF93C.Azembler
省选/NOI-
通过率:0%
时间限制:5.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
After the Search Ultimate program that searched for strings in a text failed, Igor K. got to think: "Why on Earth does my program work so slowly?" As he double-checked his code, he said: "My code contains no errors, yet I know how we will improve Search Ultimate!" and took a large book from the shelves. The book read "Azembler. Principally New Approach".
Having carefully thumbed through the book, Igor K. realised that, as it turns out, you can multiply the numbers dozens of times faster. "Search Ultimate will be faster than it has ever been!" — the fellow shouted happily and set to work.
Let us now clarify what Igor's idea was. The thing is that the code that was generated by a compiler was far from perfect. Standard multiplying does work slower than with the trick the book mentioned.
The Azembler language operates with 26 registers (eax, ebx, ..., ezx) and two commands:
- [x] — returns the value located in the address x. For example, [eax] returns the value that was located in the address, equal to the value in the register eax.
- lea x, y — assigns to the register x, indicated as the first operand, the second operand's address. Thus, for example, the "lea ebx, [eax]" command will write in the ebx register the content of the eax register: first the [eax] operation will be fulfilled, the result of it will be some value that lies in the address written in eax. But we do not need the value — the next operation will be lea, that will take the [eax] address, i.e., the value in the eax register, and will write it in ebx.
On the first thought the second operation seems meaningless, but as it turns out, it is acceptable to write the operation as
lea ecx, [eax + ebx],
lea ecx, [k*eax]
or even
lea ecx, [ebx + k*eax],
where k = 1, 2, 4 or 8.
As a result, the register ecx will be equal to the numbers eax + ebx, k*eax and ebx + k*eax correspondingly. However, such operation is fulfilled many times, dozens of times faster that the usual multiplying of numbers. And using several such operations, one can very quickly multiply some number by some other one. Of course, instead of eax, ebx and ecx you are allowed to use any registers.
For example, let the eax register contain some number that we should multiply by 41. It takes us 2 lines:
lea ebx, [eax + 4*eax] // now ebx = 5*eax
lea eax, [eax + 8*ebx] // now eax = eax + 8*ebx = 41*eax
Igor K. got interested in the following question: what is the minimum number of lea operations needed to multiply by the given number n and how to do it? Your task is to help him.
Consider that at the initial moment of time eax contains a number that Igor K. was about to multiply by n, and the registers from ebx to ezx contain number 0. At the final moment of time the result can be located in any register.
在字符串搜索程序“Search Ultimate”于文本中搜索字符串失败后,Igor K. 开始思考:“我的程序为何运行得如此之慢?”当他再次仔细检查自己的代码时,他说道:“我的代码没有任何错误,但我已经知道该如何改进‘Search Ultimate’了!”——随即从书架上取下一本厚厚的书,书名是《汇编语言:根本性新方法》(Azembler. Principally New Approach)。
Igor K. 认真翻阅了这本书后意识到,原来数字相乘的速度可以提升数十倍。“Search Ultimate 将比以往任何时候都更快!”——他兴奋地喊道,并立即投入工作。
现在我们来阐明 Igor 的想法。事实上,编译器生成的代码远非完美;标准的乘法运算比书中所提及的技巧要慢得多。
Azembler 语言拥有 26 个寄存器(eax、ebx、……、ezx)以及两条指令:
- [x] —— 返回地址 x 中存储的值。例如,[\text{eax}] 返回位于地址(该地址的值等于寄存器
eax中的值)处所存储的值。 lea _x_, _y_—— 将第二操作数 y 的地址赋给作为第一操作数指定的寄存器 x。因此,例如,指令lea ebx, [eax]将把寄存器eax中的内容写入ebx寄存器:首先执行[eax]操作,其结果为某个值(该值位于eax所存地址处);但我们并不需要这个值——接下来的lea指令将取[eax]的地址,即eax寄存器中的值,并将其写入ebx。
初看起来,第二条指令似乎毫无意义;但事实上,允许以如下形式书写该指令:
lea ecx, [eax + ebx],
lea ecx, [k*eax],
甚至
lea ecx, [ebx + k*eax],
其中 k=1,2,4 或 8。
于是,寄存器 ecx 的值将分别等于 eax + ebx、k×eax 和 ebx+k×eax。然而,此类操作的执行速度比常规的数值乘法快数十倍。通过组合若干此类操作,即可极快地实现一个数对另一个数的乘法。当然,除 eax、ebx 和 ecx 外,也可使用任意其他寄存器。
例如,设寄存器 eax 中存有某个需乘以 41 的数,则仅需两行指令:
lea ebx, [eax + 4*eax] // 此时 ebx = 5*eax
lea eax, [eax + 8*ebx] // 此时 eax = eax + 8*ebx = 41*eax
Igor K. 对如下问题产生了兴趣:将某数乘以给定整数 n 所需的最少 lea 指令条数是多少?又应如何实现? 你的任务就是帮助他解决这个问题。
请注意:初始时刻,寄存器 eax 中存有待乘以 n 的数,而寄存器 ebx 至 ezx 均为 0;最终结果可存于任意寄存器中。
输入格式
The input data contain the only integer n (1 ≤ n ≤ 255), which Igor K. is about to multiply.
输入数据包含唯一一个整数 n(1 ≤ n ≤ 255),即 Igor K. 即将要相乘的数。
输出格式
On the first line print number p, which represents the minimum number of lea operations, needed to do that. Then print the program consisting of p commands, performing the operations. It is guaranteed that such program exists for any n from 1 to 255.
Use precisely the following format of commands (here k is equal to 1, 2, 4 or 8, and x, y and z are any, even coinciding registers):
lea x, [y]
lea x, [y + z]
lea x, [k*y]
lea x, [y + k*z]
Please note that extra spaces at the end of a command are unacceptable.
第一行输出数字 p,表示完成该任务所需的最少 lea 操作次数。随后输出由 p 条指令组成的程序,执行这些操作。对于任意 n∈[1,255],保证存在满足要求的程序。
请严格使用以下指令格式(其中 k 取值为 1、2、4 或 8;x、y、z 为任意寄存器,允许重复):
lea x, [y]
lea x, [y + z]
lea x, [k*y]
lea x, [y + k*z]
注意:每条指令末尾不得包含额外空格。
输入输出样例
输入#1
41
输出#1
2 lea ebx, [eax + 4*eax] lea ecx, [eax + 8*ebx]
输入#2
2
输出#2
1 lea ebx, [eax + eax]
输入#3
4
输出#3
1 lea ebx, [4*eax]
输入解题思路,AI测评打分。不知道怎么写?