#P5698. [CTSC1998] 算法复杂度
[CTSC1998] 算法复杂度
Background
CTSC1998 D1T3.
When we do programming, the issue we care about most is the time complexity of an algorithm. However, analyzing the complexity of a program is very difficult, and it becomes even harder when the program is not well written.
So, the SERKOI group, which specializes in studying algorithms, decided to develop software to analyze the time complexity of a program. Since this is a brand-new field, the SERKOI group decided to start with simple cases first.
Problem Description
To simplify the problem, the program contains only loops and sequential structures. The program structure is defined as follows:
The structure of a statement block is recursively defined as follows:
or
or
or
A statement block can be empty.
Notes:
-
A program starts with and ends with the corresponding .
-
means the statements inside are executed repeatedly times.
-
means performing unit operations.
-
In the above two points, can be a positive integer or .
-
The statement exits the current loop level, and the statement skips the remaining statements in the current loop level and directly enters the next iteration. If it ( or ) is not inside any loop, please ignore them.
You need to write a program to compute the time complexity of the program described above, and output it in polynomial form.
Note that this polynomial is a polynomial in , and the constant term and coefficients cannot be omitted.
The testdata guarantees that the time complexity of the program can be determined.
Input Format
The input contains a complete program.
Between every two statements, there is at least one space or newline character.
Output Format
Output the computed time complexity polynomial in descending order of degree.
begin loop n loop 3 loop n
op 20
end end end
loop n op 3 break end
loop n loop n
op 1
break
end end
end
60n^2+n+3
begin
op n
loop 3
op n
break
end
loop n
loop n
op 1
continue
op n
end
end
end
n^2+2n
Hint
The loop nesting depth is at most levels.
It is guaranteed that the coefficient of each term in the final time complexity polynomial does not exceed .
Translated by ChatGPT 5