Hide

Problem A
Well-Defined Functions

In this problem, let’s refer to a function as well-defined if its value can be computed for any input.

Let’s restrict ourselves to functions that take a single positive integer, $n$, and produce a single integer. For example, $f(n) = 3 + n$ is such a function, and it’s obviously well-defined. The functions we consider may be recursive, or depend on other functions. Another function $g(n) = f(n-1) + f(n-2)$, is also well-defined since $f$ is well-defined.

Consider the function $h(n) = h(n-2) + 1$. This is not well-defined, unless $h$ has a base case. For simplicity, we’ll assume that all functions we consider have a base case that gives $0$ whenever the input $n \leq 0$ (even for the $f(n)$ and $g(n)$ we considered above). Now that we have this base case, we can see that $h(n) = \lceil n/2\rceil $.

Even with a base case for every function, it’s still possible to give functions which are not well-defined – that is, they don’t have a solution for some $n$. The two ways a function may be not well-defined is if it will never stop computing (i.e. never reach some base case), or if it depends on another function which is not defined.

Input

Input has up to $100$ sets of functions. Each set of functions starts with a line containing an integer $1 \le m \le 100$ defining the number of function definitions that follow. The next $m$ lines each contain one function definition, of the form

name(n) = series

where ‘name’ is the name of the function (one to $10$ lowercase a–z characters). The ‘(n) = ’ is always the same. To the right side of the equals sign is a space-separated series of additions and subtractions of one to $20$ terms. Each term is either a function call, a constant, or the variable n. The argument to each function call may be ‘n’, ‘n+x’, or ‘n-x’ (no spaces). All integer constants are in the range $1$ to $1\, 000$. Input ends at the end of file.

Each set of functions should be considered independent of every other set. Within a set, no function is defined more than once. No function is ever named ‘n’.

Output

For each set of functions, output well-defined if every function in the set is well-defined. If any function in the set is not well-defined, output not well-defined followed by a list of the functions which are not well-defined, in lexicographic order.

Sample Input 1 Sample Output 1
1
foo(n) = foo(n-1) + 2
2
bar(n) = foo(n+1) + 5 + n
foo(n) = bar(n-1) - 23
2
m(n) = m(n-1) + q(n-2) + 7
q(n) = m(n) + 3
1
f(n) = g(n-1)
well-defined
not well-defined bar foo
well-defined
not well-defined f

Please log in to submit a solution to this problem

Log in