Zero Division Checker
Time limit2sMemory limit512 MB
Given variable ranges, decide whether some assignment causes a division by zero in an 8-bit reverse Polish notation evaluation.
- Level
Medium6 of 10
- Topics
- Stack, Brute force, Implementation, Math
- Solved
- No attempts yet
Problem
Morishita is in trouble. Anehara wrote a program for him, and it crashes.
The program Anehara wrote reads a reverse Polish notation expression and prints the result of evaluating it. The crash log suggests the cause is a division by zero. Maybe Morishita entered a bad expression, or maybe, just maybe, Anehara's program has a bug.
Morishita thought about reading Anehara's program, but it seems to be written in assembly, a language he does not understand, and just looking at it gives him a pounding headache.
So Morishita decided to have you write a program that checks whether an expression is bad. An expression is bad when evaluating it can divide by zero.
Anehara's code runs on a very old computer: the arithmetic is done on integers, and results are stored as 8-bit unsigned integers. For example, 255+1 becomes 0, and 3/2 becomes 1.
Oh, don't tell Anehara.
(Reference) Pseudocode that evaluates an expression in reverse Polish notation
s = empty stack
n = number of elements in the expression
for i in 1..n:
if the i-th element of the expression is an integer:
push that integer onto s
if the i-th element of the expression is a variable:
push the value of that variable onto s
if the i-th element of the expression is an operator:
pop a value from s and call it b
pop a value from s and call it a
if the operator is '+':
let r = (a + b) % 256
if the operator is '-':
let r = (a - b + 256) % 256
if the operator is '*':
let r = (a * b) % 256
if the operator is '/':
let r = (a / b) % 256
push r onto s
pop a value from s and take it as the result of evaluating the expression
Input
m
name1 lb1 ub1
...
namem lbm ubm
n
e1 e2 ... en
0 ≤ m ≤ 1000 ≤ lbi ≤ ubi ≤ 2551 ≤ length of namei (1 ≤ i ≤ m) ≤ 201 ≤ n ≤ 100mis the number of variables, andnamei,lbi,ubiare the name, lower bound, and upper bound of variable i.nis the number of elements in the expression, andeiis the i-th element of the expression.- Each variable appears at most once in the expression.
Output
Print error in one line if the expression is bad, or correct if it is not.