C. Short Program

Codeforces
IDCF879C
Time2000ms
Memory256MB
Difficulty
bitmasksconstructive algorithmsgraph matchings
English · Original
Chinese · Translation
Formal · Original
Petya learned a new programming language CALPAS. A program in this language always takes one non-negative integer and returns one non-negative integer as well. In the language, there are only three commands: apply a bitwise operation AND, OR or XOR with a given constant to the current integer. A program can contain an arbitrary sequence of these operations with arbitrary constants from 0 to 1023. When the program is run, all operations are applied (in the given order) to the argument and in the end the result integer is returned. Petya wrote a program in this language, but it turned out to be too long. Write a program in CALPAS that does the same thing as the Petya's program, and consists of no more than 5 lines. Your program should return the same integer as Petya's program for all arguments from 0 to 1023. ## Input The first line contains an integer _n_ (1 ≤ _n_ ≤ 5·105) — the number of lines. Next _n_ lines contain commands. A command consists of a character that represents the operation ("_&_", "_|_" or "_^_" for AND, OR or XOR respectively), and the constant _x__i_ 0 ≤ _x__i_ ≤ 1023. ## Output Output an integer _k_ (0 ≤ _k_ ≤ 5) — the length of your program. Next _k_ lines must contain commands in the same format as in the input. [samples] ## Note You can read about bitwise operations in [https://en.wikipedia.org/wiki/Bitwise_operation](https://en.wikipedia.org/wiki/Bitwise_operation). Second sample: Let _x_ be an input of the Petya's program. It's output is ((_x_&1)&3)&5 = _x_&(1&3&5) = _x_&1. So these two programs always give the same outputs.
Petya 学习了一种名为 CALPAS 的新编程语言。这种语言的程序总是接受一个非负整数,并返回一个非负整数。 在该语言中,仅有三种指令:将给定的常数与当前整数进行按位与(AND)、或(OR)或异或(XOR)操作。程序可以包含任意数量的这些操作,常数范围为 #cf_span[0] 到 #cf_span[1023]。当程序运行时,所有操作按给定顺序依次作用于输入整数,最终返回结果整数。 Petya 编写了一个该语言的程序,但程序过长。请编写一个 CALPAS 程序,其功能与 Petya 的程序完全相同,且指令数不超过 #cf_span[5] 行。你的程序对于所有从 #cf_span[0] 到 #cf_span[1023] 的输入,必须返回与 Petya 程序相同的整数。 第一行包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 5·105]) —— 表示指令行数。 接下来 #cf_span[n] 行为指令。每条指令由一个表示操作的字符("_&_"、"_|_" 或 "_^_" 分别代表 AND、OR 或 XOR)和一个常数 #cf_span[xi] (#cf_span[0 ≤ xi ≤ 1023]) 组成。 请输出一个整数 #cf_span[k] (#cf_span[0 ≤ k ≤ 5]) —— 你的程序的长度。 接下来 #cf_span[k] 行必须以与输入相同的格式输出指令。 你可以阅读关于按位操作的更多信息:https://en.wikipedia.org/wiki/Bitwise_operation。 第二个样例: 设 #cf_span[x] 为 Petya 程序的输入,其输出为 #cf_span[((x&1)&3)&5 = x&(1&3&5) = x&1]。因此这两个程序对所有输入均给出相同结果。 ## Input 第一行包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 5·105]) —— 表示指令行数。接下来 #cf_span[n] 行为指令。每条指令由一个表示操作的字符("_&_"、"_|_" 或 "_^_" 分别代表 AND、OR 或 XOR)和一个常数 #cf_span[xi] (#cf_span[0 ≤ xi ≤ 1023]) 组成。 ## Output 请输出一个整数 #cf_span[k] (#cf_span[0 ≤ k ≤ 5]) —— 你的程序的长度。接下来 #cf_span[k] 行必须以与输入相同的格式输出指令。 [samples] ## Note 你可以阅读关于按位操作的更多信息:https://en.wikipedia.org/wiki/Bitwise_operation。第二个样例:设 #cf_span[x] 为 Petya 程序的输入,其输出为 #cf_span[((x&1)&3)&5 = x&(1&3&5) = x&1]。因此这两个程序对所有输入均给出相同结果。
**Definitions** Let $ n \in \mathbb{Z} $ be the number of commands in Petya’s program. Let $ C = \{(op_i, x_i) \mid i \in \{1, \dots, n\}\} $ be the sequence of commands, where $ op_i \in \{\&, |, \oplus\} $ and $ x_i \in \{0, 1, \dots, 1023\} $. Let $ f: \{0, 1, \dots, 1023\} \to \{0, 1, \dots, 1023\} $ be the function computed by Petya’s program: $$ f(x) = op_n(x_n, op_{n-1}(x_{n-1}, \dots op_1(x_1, x) \dots )) $$ **Constraints** 1. $ 1 \leq n \leq 5 \cdot 10^5 $ 2. $ 0 \leq x_i \leq 1023 $ for all $ i $ **Objective** Find a program with $ k \leq 5 $ commands $ \{(op'_j, x'_j) \mid j \in \{1, \dots, k\}\} $ such that for all $ x \in \{0, 1, \dots, 1023\} $: $$ f(x) = op'_k(x'_k, op'_{k-1}(x'_{k-1}, \dots op'_1(x'_1, x) \dots )) $$ Moreover, since all operations are bitwise and constants are 10-bit, the function $ f $ is equivalent to: $$ f(x) = (x \mathbin{\text{op}_1} x_1) \mathbin{\text{op}_2} x_2 \mathbin{\text{op}_3} \dots \mathbin{\text{op}_n} x_n $$ and due to the linearity and idempotence properties of bitwise operations, $ f(x) $ can be rewritten as: $$ f(x) = (x \& A) | (B) \oplus (C) $$ for some constants $ A, B, C \in \{0, 1, \dots, 1023\} $, computable by evaluating $ f $ on three basis inputs: $ x = 0 $, $ x = 1023 $, and $ x = 1 \ll i $ for each bit $ i \in \{0, \dots, 9\} $. We can compress any sequence of AND, OR, XOR operations into at most 3 operations: $$ f(x) = ((x \& A) \mid B) \oplus C $$ and further reduce to at most 5 operations if needed, but optimally to 3. Thus, compute constants $ A, B, C $ such that: - $ A $: bits preserved (AND mask) - $ B $: bits forced to 1 (OR mask) - $ C $: bits flipped (XOR mask) Then output the minimal sequence of up to 5 operations that implements $ f(x) = ((x \& A) | B) \oplus C $, using at most one of each operation type, in optimal order. **Output** Find $ k \in \{0, 1, 2, 3, 4, 5\} $ and $ k $ commands minimizing the program length while preserving $ f(x) $ for all $ x \in [0, 1023] $.
Samples
Input #1
3
| 3
^ 2
| 1
Output #1
2
| 3
^ 2
Input #2
3
& 1
& 3
& 5
Output #2
1
& 1
Input #3
3
^ 1
^ 2
^ 3
Output #3
0
API Response (JSON)
{
  "problem": {
    "name": "C. Short Program",
    "description": {
      "content": "Petya learned a new programming language CALPAS. A program in this language always takes one non-negative integer and returns one non-negative integer as well. In the language, there are only three c",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF879C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Petya learned a new programming language CALPAS. A program in this language always takes one non-negative integer and returns one non-negative integer as well.\n\nIn the language, there are only three c...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Petya 学习了一种名为 CALPAS 的新编程语言。这种语言的程序总是接受一个非负整数,并返回一个非负整数。\n\n在该语言中,仅有三种指令:将给定的常数与当前整数进行按位与(AND)、或(OR)或异或(XOR)操作。程序可以包含任意数量的这些操作,常数范围为 #cf_span[0] 到 #cf_span[1023]。当程序运行时,所有操作按给定顺序依次作用于输入整数,最终返回结果整数。\n\nPet...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z} $ be the number of commands in Petya’s program.  \nLet $ C = \\{(op_i, x_i) \\mid i \\in \\{1, \\dots, n\\}\\} $ be the sequence of commands, where $ op_i \\in \\{\\&, |,...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments