C. Azembler

Codeforces
IDCF93C
Time5000ms
Memory256MB
Difficulty
brute forceimplementation
English · Original
Chinese · Translation
Formal · Original
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. ## Input The input data contain the only integer _n_ (1 ≤ _n_ ≤ 255), which Igor K. is about to multiply. ## Output 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**. [samples]
在用于在文本中搜索字符串的 Search Ultimate 程序失败后,Igor K. 开始思考:"为什么我的程序运行得如此缓慢?" 在复查代码时,他说道:"我的代码没有错误,但我已知道如何改进 Search Ultimate!" 并从书架上取下一本厚书。这本书名为《Azembler:全新方法》。 仔细翻阅这本书后,Igor K. 意识到,正如书中所述,你可以将数字相乘的速度提高数十倍。"Search Ultimate 将比以往任何时候都更快!" 他高兴地喊道,并立即投入工作。 现在让我们澄清 Igor 的想法。编译器生成的代码远非完美。标准的乘法运算比书中提到的技巧慢得多。 Azembler 语言使用 26 个寄存器(eax、ebx、...、ezx)和两条指令: 乍一看,第二条指令似乎毫无意义,但事实上,它可以写成: 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. 对以下问题产生了兴趣:将给定数字 #cf_span[n] 相乘所需的最少 lea 操作次数是多少?如何实现?你的任务是帮助他。 假设在初始时刻,eax 包含 Igor K. 要乘以 #cf_span[n] 的数,而从 ebx 到 ezx 的寄存器均包含数字 0。在最终时刻,结果可以位于任意寄存器中。 输入数据包含一个整数 #cf_span[n](#cf_span[1 ≤ n ≤ 255]),即 Igor K. 要乘的数。 第一行输出数字 #cf_span[p],表示完成该乘法所需的最少 lea 操作次数。然后输出包含 #cf_span[p] 条指令的程序。保证对于 1 到 255 的任意 #cf_span[n],这样的程序都存在。 请严格使用以下指令格式(其中 #cf_span[k] 等于 1、2、4 或 8,#cf_span[x]、#cf_span[y] 和 #cf_span[z] 可为任意寄存器,甚至可以相同): lea x, [y] lea x, [y + z] lea x, [k*y] lea x, [y + k*z] 请注意,*指令末尾的额外空格是不允许的*。 ## Input 输入数据包含一个整数 #cf_span[n](#cf_span[1 ≤ n ≤ 255]),即 Igor K. 要乘的数。 ## Output 第一行输出数字 #cf_span[p],表示完成该乘法所需的最少 lea 操作次数。然后输出包含 #cf_span[p] 条指令的程序。保证对于 1 到 255 的任意 #cf_span[n],这样的程序都存在。请严格使用以下指令格式(其中 #cf_span[k] 等于 1、2、4 或 8,#cf_span[x]、#cf_span[y] 和 #cf_span[z] 可为任意寄存器,甚至可以相同):lea x, [y]lea x, [y + z]lea x, [k*y]lea x, [y + k*z]请注意,*指令末尾的额外空格是不允许的*。 [samples]
**Definitions** Let $ n \in \mathbb{Z} $, $ 1 \leq n \leq 255 $, be the target multiplier. Let $ R = \{ \text{eax}, \text{ebx}, \dots, \text{ezx} \} $ be the set of 26 registers. Initially, $ \text{eax} = a $ (input value), and all other registers are 0. Each `lea` operation updates one register to a linear combination of two others with coefficients in $ \{1, 2, 4, 8\} $: - $ r_i \gets r_j $ - $ r_i \gets r_j + r_k $ - $ r_i \gets c \cdot r_j $, where $ c \in \{1,2,4,8\} $ - $ r_i \gets r_j + c \cdot r_k $, where $ c \in \{1,2,4,8\} $ **Constraints** 1. Only `lea` operations are allowed. 2. Coefficients in linear combinations must be from $ \{1,2,4,8\} $. 3. Initial state: $ \text{eax} = a $, all other registers = 0. 4. Final state: some register must contain $ n \cdot a $. **Objective** Find the minimum number $ p $ of `lea` operations to compute $ n \cdot a $, and output a sequence of $ p $ valid `lea` commands achieving this.
Samples
Input #1
41
Output #1
2
lea ebx, \[eax + 4*eax\]
lea ecx, \[eax + 8*ebx\]
Input #2
2
Output #2
1
lea ebx, \[eax + eax\]
Input #3
4
Output #3
1
lea ebx, \[4*eax\]
API Response (JSON)
{
  "problem": {
    "name": "C. Azembler",
    "description": {
      "content": "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 cont",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 5000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF93C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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 cont...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "在用于在文本中搜索字符串的 Search Ultimate 程序失败后,Igor K. 开始思考:\"为什么我的程序运行得如此缓慢?\" 在复查代码时,他说道:\"我的代码没有错误,但我已知道如何改进 Search Ultimate!\" 并从书架上取下一本厚书。这本书名为《Azembler:全新方法》。\n\n仔细翻阅这本书后,Igor K. 意识到,正如书中所述,你可以将数字相乘的速度提高数十倍。\"Sea...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z} $, $ 1 \\leq n \\leq 255 $, be the target multiplier.  \nLet $ R = \\{ \\text{eax}, \\text{ebx}, \\dots, \\text{ezx} \\} $ be the set of 26 registers.  \nInitially, $ \\t...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments