#P2229. Ancient City’s Enigma
Ancient City’s Enigma
Ancient City’s Enigma
Deep in the endless desert lie the ruins of an ancient city, rumored to be filled with treasures beyond imagining. Many have set out to explore these ruins after hearing the legend – yet none have ever returned. Among them, the young and brilliant explorer Jack stands out. Despite joining the team only recently, his wit has saved his comrades from countless perils. Under the motto “When you know the tiger is on the mountain, you still dare to climb”, Jack leads his team into the heart of the desert in search of treasure.
At last, a team member shouts, “I found the ruins!” Jack soon makes out the remnants of ancient walls and a mysterious passage. Convinced that they have reached the fabled place, Jack leads everyone into the passage. But fate intervenes – the passage entrance suddenly slams shut behind them. At its end stands a colossal iron gate with an advanced electronic lock, complete with a keyboard and display. Carved beside it is a mocking instruction:
"Those who wish to claim my treasure have trudged a hard path to reach here. But your suffering ends now. Only if you compute the result of the program displayed on my screen and input it via the keyboard will the door open – and with it, all my treasures shall be yours."
Jack, ever resourceful, pulls out his cutting‐edge notebook computer. With a confident smile he declares, “Haven’t you seen one of these? Without it, even my genius couldn’t crack this door.” And so the challenge is set before you.
The treasure owner’s program is unusual. Though no one has seen it before, Jack’s educated guess is that its structure is as follows:
- The program consists of the following statements:
start
and its matchingend
(marking the beginning and end of the entire program).loop <expr>
— the expression evaluates to an integer N indicating that the following block is to be executed N times. Its loop is ended by a matchingend
.continue
— immediately jump to the current loop’s matchingend
statement, ending the current iteration.break
— exit the current loop immediately, skipping any remaining iterations.write <expr>
— output the value of the expression to be used as part of the password.- Assignment statements in the form
v=<expr>
, wherev
is a single lowercase letter (a
–z
). This assigns the evaluated value of the expression to variablev
.
- Expressions consist only of digits (multi-digit permitted), variables (
a
–z
), the four operators+
,-
,*
,/
and parentheses. All operations are integer operations (with division meaning integer division). You may assume all variables are initialized to 0. - The total number of statement executions is less than 2,000,000.
Your task is to simulate the execution of this program and output exactly what the write
statements print. Each output should appear on its own line.
inputFormat
The input is a program consisting of several lines. The program always starts with a start
statement and ends with its matching end
. Each statement is one of the following:
start
end
loop <expression>
continue
break
write <expression>
- Assignment:
v=<expression>
wherev
is a lowercase letter
The expression in loop
, write
, and assignment statements may contain digits, variables (a–z), parentheses, and the operators +
, -
, *
and /
. There will be no extra spaces except for the one following loop
or write
as specified.
outputFormat
Output the values produced by the write
statements in the order of their execution, each on a separate line.
sample
start
x=5+3
write x
end
8