Eventually Periodic Sequence: N. F F (N) and F
Eventually Periodic Sequence: N. F F (N) and F
Input
Each line of input contains N , n and the a description of f in postfix notation, also known as Reverse
Polish Notation (RPN). The operands are either unsigned integer constants or N or the variable x.
Only binary operands are allowed: ‘+’ (addition), ‘*’ (multiplication) and ‘%’ (modulo, i.e. remainder of
integer division). Operands and operators are separated by whitespace. The operand % occurs exactly
once in a function and it is the last (rightmost, or topmost if you wish) operator and its second operand
is always N whose value is read from input. The following function:
2 x * 7 + N %
is the RPN rendition of the more familiar infix ‘(2*x+7)%N’. All input lines are shorter than 100 char-
acters. The last line of input has N equal ‘0’ and should not be processed.
Output
For each line of input, output one line with one integer number, the period of F corresponding to the
data given in the input line.
Sample Input
10 1 x N %
11 1 x x 1 + * N %
1728 1 x x 1 + * x 2 + * N %
1728 1 x x 1 + x 2 + * * N %
100003 1 x x 123 + * x 12345 + * N %
0 0 0 N %
Sample Output
1
3
6
Universidad de Valladolid OJ: 11036 – Eventually periodic sequence 2/2
6
369