K-Calculator
Time limit1.5sMemory limit512 MB
Evaluate an expression of rational operands by repeatedly applying the operator selected by u_i XOR the modular value of the previous result, removing it and merging its operands.
- Level
Hard8 of 10
- Topics
- Linked list, Math, Number theory, Simulation
- Solved
- No attempts yet
Problem

Do you know kimchi?
Do you know Lee Guk-ryeol?
Do you know calculators? Perfect
Guk-ryeol has set a calculator problem three times already, and now he has set another one. Let us see how long this keeps going. You have run into yet another calculator problem, which is annoying, but to win the prize you have to evaluate the given expression according to the rules.
Guk-ryeol gave expressions in the same format three times, and since Guk-ryeol, the contestants, and the reviewers all do not want to suffer through parsing, he changed the format. In this input, numbers and operators alternate, with a space between each number and operator. If we write the -th number of the expression as and the -th operator as , an expression with numbers can be written as ... . The operators are +, -, *, /. The input never ends with an operator, never has a negative , and never has unnecessary leading zeros. That is, cases such as , , and are not given as input.
An expression made of numbers and positive integers are given. You evaluate it according to the following rules.
- Repeat the following times.
- Take the most recently computed result, written as the reduced fraction . Let be the multiplicative inverse of modulo , and let be the remainder of modulo . If there is no previously computed result, .
- Evaluate the -th operator and output the result of that evaluation. Here means bitwise XOR.
- Remove the operator and operands you evaluated, and put the result in their place.
For example, given the expression and the positive integers , you evaluate as follows.
- Since , evaluate the 1st operator. The result is , so output , and the expression becomes .
- Since , evaluate the 2nd operator. The result is , so output , and the expression becomes .
- Since , evaluate the 1st operator. The result is , so output .
Division in this problem is real division. That is, is , not .
Write a program that evaluates an expression this way.
Input
The first line gives , the number of operands. ()
The second line gives an expression made only of operands and +, -, *, /. Operands and operators are separated by spaces, and each operand is an integer between and inclusive.
The third line gives , ..., , separated by spaces. ()
No case in the evaluation divides by .
Output
For each , write the result for as the reduced fraction , let be the multiplicative inverse of modulo , and print the remainder of modulo on the -th line.