#P8997. [CEOI 2022] Homework

[CEOI 2022] Homework

Problem Description

This is a problem from Helena’s math homework.

We define valid expressions as follows:

  • ? is a valid expression, representing an unknown.
  • If A,BA, B are both valid expressions, then min(AA,BB) and max(AA,BB) are both valid expressions, representing taking the minimum / maximum of the left and right sides, respectively.

Let the number of ? be NN. Now a valid expression is given. If we replace each question mark with any number from 11 to NN, and each number can be used at most once, how many different final results can be obtained?

Poor Helena cannot solve it. Please help her.

Input Format

A single line containing a string, representing the given valid expression.

Output Format

Output one integer, representing the number of distinct results.

min(min(?,?),min(?,?))
1
max(?,max(?,min(?,?)))
2
min(max(?,?),min(?,max(?,?)))
3

Hint

Sample 1 Explanation

No matter how the values are chosen, the final result will be min{1,2,3,4}\min\{1,2,3,4\}, i.e. 11.

Sample 2 Explanation

One assignment that yields the answer 44 is: 4=max(4,max(3,min(2,1))).
One assignment that yields the answer 33 is: 3=max(3,max(2,min(1,4))).
It can be proven that the answer cannot be 11 or 22.

Constraints

For all testdata, 2N1062\le N\le 10^6.

Subtask ID Special Constraints Score
11 N9N\le 9 1010
22 N16N\le 16 1313
33 For any min(AA,BB) and max(AA,BB), one of AA and BB is ?.
44 N103N\le 10^3 3030
55 No special constraints. 3434

Translated by ChatGPT 5