F. 2 + 2 != 4

Codeforces
IDCF952F
Time1000ms
Memory256MB
Difficulty
English · Original
Chinese · Translation
Formal · Original
One very experienced problem writer decided to prepare a problem for April Fools Day contest. The task was very simple - given an arithmetic expression, return the result of evaluating this expression. However, looks like there is a bug in the reference solution... ## Input The only line of input data contains the arithmetic expression. The expression will contain between 2 and 10 operands, separated with arithmetic signs plus and/or minus. Each operand will be an integer between 0 and 255, inclusive. ## Output Reproduce the output of the reference solution, including the bug. [samples]
一位非常有经验的题目出题人决定为愚人节比赛准备一道题。任务非常简单——给定一个算术表达式,返回该表达式的计算结果。然而,参考程序似乎存在一个 bug... 输入数据仅有一行,包含一个算术表达式。表达式将包含 2 到 10 个操作数,由加号和/或减号分隔。每个操作数均为 0 到 255(含)之间的整数。 请复现参考程序的输出,包括其 bug。 ## Input The only line of input data contains the arithmetic expression. The expression will contain between 2 and 10 operands, separated with arithmetic signs plus and/or minus. Each operand will be an integer between 0 and 255, inclusive. ## Output Reproduce the output of the reference solution, including the bug. [samples]
**Definitions** Let $ E $ be an arithmetic expression of the form: $$ a_1 \circ_1 a_2 \circ_2 a_3 \circ_3 \cdots \circ_{n-1} a_n $$ where: - $ n \in \mathbb{Z} $, $ 2 \leq n \leq 10 $, is the number of operands, - $ a_i \in \mathbb{Z} $, $ 0 \leq a_i \leq 255 $, for all $ i \in \{1, \dots, n\} $, - $ \circ_j \in \{+, -\} $ for all $ j \in \{1, \dots, n-1\} $. **Constraints** 1. The expression is given as a single string with no parentheses, no whitespace, and operands and operators concatenated directly. 2. All operands are non-negative integers in $[0, 255]$. 3. Operators are only `+` and `-`. **Objective** Reproduce the output of a reference solution with the following bug: > The expression is evaluated **left-to-right**, ignoring standard operator precedence, **but** the first operator is **always treated as addition**, regardless of its actual symbol. That is, define the evaluation function $ f(E) $ as: $$ f(E) = a_1 \oplus a_2 \oplus a_3 \oplus \cdots \oplus a_n $$ where: - $ \oplus $ denotes left-to-right binary operation, - The operation between $ a_1 $ and $ a_2 $ is **always addition**, i.e., $ a_1 + a_2 $, - For $ i \geq 3 $, the operation between the accumulated result and $ a_i $ is determined by $ \circ_{i-1} $: $$ \text{result} = \begin{cases} \text{result} + a_i & \text{if } \circ_{i-1} = + \\ \text{result} - a_i & \text{if } \circ_{i-1} = - \end{cases} $$ **Note**: The bug is that the **first operator $ \circ_1 $ is ignored** and replaced with `+`, even if it is `-`.
Samples
Input #1
8-7+6-5+4-3+2-1-0
Output #1
4
Input #2
2+2
Output #2
\-46
Input #3
112-37
Output #3
375
API Response (JSON)
{
  "problem": {
    "name": "F. 2 + 2 != 4",
    "description": {
      "content": "One very experienced problem writer decided to prepare a problem for April Fools Day contest. The task was very simple - given an arithmetic expression, return the result of evaluating this expression",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF952F"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "One very experienced problem writer decided to prepare a problem for April Fools Day contest. The task was very simple - given an arithmetic expression, return the result of evaluating this expression...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "一位非常有经验的题目出题人决定为愚人节比赛准备一道题。任务非常简单——给定一个算术表达式,返回该表达式的计算结果。然而,参考程序似乎存在一个 bug...\n\n输入数据仅有一行,包含一个算术表达式。表达式将包含 2 到 10 个操作数,由加号和/或减号分隔。每个操作数均为 0 到 255(含)之间的整数。\n\n请复现参考程序的输出,包括其 bug。\n\n## Input\n\nThe only line o...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ E $ be an arithmetic expression of the form:  \n$$\na_1 \\circ_1 a_2 \\circ_2 a_3 \\circ_3 \\cdots \\circ_{n-1} a_n\n$$  \nwhere:  \n- $ n \\in \\mathbb{Z} $, $ 2 \\leq n \\leq 10 $, is the ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments