Divisors
Time limit2sMemory limit512 MB
Given n and an expression built from divisors of n with gcd and lcm, decide whether the expression's value is the same for every assignment of variables.
- Level
Hard8 of 10
- Topics
- Number theory, Tree, Math, Recursion
- Solved
- No attempts yet
Problem
We are interested in the divisors of a given natural number . Let denote the set of all divisors of . We are given an expression built from constants taken from , variables that may take any value in , and two binary functions that compute the greatest common divisor and the least common multiple.
For a given expression, decide whether its value is the same for every possible assignment of values to the variables, that is, whether the expression denotes a constant function.
Input
The first line contains an integer (), the number of test cases. Each of the next lines describes one test case.
Each description starts with an integer (), followed by the description of the expression. An expression is a constant, a variable, or a function application.
- Each number is a constant and is a positive divisor of .
- A variable is a sequence of at most lowercase English letters; identical sequences denote the same variable.
- The names
NWDandNWWdenote the functions computing the greatest common divisor and the least common multiple, respectively. A function name is followed by a single space and then the space-separated descriptions of its two arguments, each of which is itself an expression (so the description is recursive).
The total size of the input does not exceed MB.
Output
Print lines, one answer per test case. For each test case print TAK (Polish for yes) if the expression denotes a constant function regardless of the variable values, and NIE (Polish for no) otherwise.