Questions
Time limit1sMemory limit128 MB
Simulate a logic puzzle where princes and a sorcerer reason about a system of variable constraints over time; answer what each prince or the sorcerer knows.
- Level
Hard9 of 10
- Topics
- Brute force, Simulation, Combinatorics, Implementation
- Solved
- No attempts yet
Problem
Long ago, King Bytour invented a game for his three sons and described it to his counsellor, the sorcerer Bytelean:
"I told my three sons (numbered 1, 2, 3) to stand in a row, and I placed a golden or a silver crown on each of their heads. Son 1 could see the crowns of sons 2 and 3, and son 2 could see the crown of son 3. Every son knew that there were at most two golden crowns in total. I then asked son 1 whether he knew the colour of his own crown; he answered that he did not. I asked son 2 the same question, and he also answered that he did not."
At this moment Bytelean interrupted the king and said that he already knew which crown prince 3 had received. Asked how, he explained:
"If son 1 had seen two golden crowns, he would have known that his own was silver (there are at most two golden crowns). He answered NO, so that situation is impossible. Now, if son 2 had seen a golden crown on son 3's head, he would have known that his own was silver, because otherwise son 1 would have answered YES. Son 2 did not know either, so prince 3 must have a silver crown."
Your task is to implement a general simulator of such situations. The facts the king may ask the princes and the sorcerer about (the crown colour, in the story) are encoded as a sequence of variables. Some variables are derived from earlier ones; for the rest, only a range of possible values is known. The exact format is described in the Input section.
Write a program that reads a description of a situation from standard input, computes the answers the sorcerer should give, and writes them to standard output.
Input
The first line contains three integers , , and , separated by single spaces. is the number of princes (numbered to ), is the number of variables (numbered to ), and is the number of actions. The constraints , , and hold.
The next lines describe the variables . Each line has the form Z A B (single spaces), where is one of the characters =, +, -, *, /, %, >, and , are integers. The meaning depends on :
This information is given at the start of the game to all princes and to the sorcerer.
The next lines describe the actions (single spaces):
S g n: The value of is revealed to prince . The fact that this value was revealed to prince becomes known to all princes and to the sorcerer, but the value itself does not.T g n: The king asks prince whether he knows the value of . The answer is YES, and the king passes it to the sorcerer. The other princes learn this answer only when anAaction is performed, so several princes can answer "at the same time" without their answers influencing one another.N g n: The same as above, but the answer is NO.X g n: The same question is asked, but the king does not pass the prince's answer to the sorcerer. Instead he asks the sorcerer to guess it. The sorcerer answers YES, NO, or I DON'T KNOW (I DON'T KNOW meaning the sorcerer cannot tell whether prince knew the answer). During this action the king does not reveal prince 's real answer to the sorcerer, and does not pass the sorcerer's guess to the princes. (The sorcerer's answers are written in Polish, see Output.)A 0 0: Every prince is told the answers that all other princes gave to every king's question asked since the previousAaction (theT,N, andXquestions). During this action the king does not tell the sorcerer the answers he received inXactions.M w n: The king tells the sorcerer that variable has value .Q 0 n: The king asks the sorcerer which values may take, according to his current knowledge. The sorcerer's answer is not passed to the princes.
Both the princes and the sorcerer reason perfectly: at every moment each of them can deduce every fact implied by the initial ranges and by everything that has happened so far. Moreover, each of them knows that all of them reason perfectly.
Additional guarantees: the number of possible valuations (the product of over all variables of type =) does not exceed . In every valuation that is theoretically possible, the absolute value of each variable is at most ; for the operations / and %, is non-negative and is positive. A variable may appear in the definition of only if .
Output
For every X action, print one line containing TAK (YES), NIE (NO), or NIE WIEM (I DON'T KNOW).
For every Q action, print one line containing all values that may take, from the smallest to the largest, separated by single spaces.
The lines must appear in the same order as the actions they correspond to, independently of the action types.