Shipura
Time limit8sMemory limit512 MB
Evaluate expressions that combine floor division by powers of two with squaring modulo 1,000,000,007 and nested brackets.
- Level
Medium4 of 10
- Topics
- Stack, Recursion, Bit manipulation
- Solved
- No attempts yet
Problem
Dr. Suposupo built a programming language called Shipura. Shipura has one binary operator, >>, and one unary function, S< >, and nothing else.
>> evaluates to , the greatest integer that does not exceed . S< > evaluates to , the remainder of divided by .
The operator >> is left associative. For example, >> >> is read as >> >> , not as >> >> . These parentheses never appear in an actual Shipura expression.
The syntax of Shipura in BNF is the following.
expr ::= term | expr sp ">>" sp term
term ::= number | "S" sp "<" sp expr sp ">"
sp ::= "" | sp " "
number ::= digit | number digit
digit ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
The start symbol is expr, which represents a Shipura expression. A number is an integer between and inclusive, written without extra leading zeros.
Write a program that evaluates Shipura expressions.
Input
The input is a sequence of datasets. Each dataset is one line, and that line holds one valid Shipura expression.
A line holding a single # ends the input. There are at most 100 datasets, and the whole input file is at most 2,000,000 bytes.
Output
For each dataset, print the value of the expression on one line.