Add or Multiply
Time limit7sMemory limit1024 MB
Given a digit expression with + and × operators, report its value modulo 1e9+7 after each swap of digits, single operator flip, or flip of all operators.
- Level
Medium6 of 10
- Topics
- Segment tree, Math
- Solved
- No attempts yet
Problem
Your younger sister is playing with a wooden math toy made of number blocks and operator blocks. Each number block shows a single digit from 1 to 9. Each operator block is double-sided, and each side shows + or ×.
She has built a math expression by alternating number and operator blocks. The first block is a number, the second is an operator, the third is a number, and so on. She asks whether you can find the value of the expression.
She then repeatedly makes one of the following moves.
s(swap): Swaps the th and th number blocks.f(flip): Flips the th operator block. Its operator alternates between + and ×.a(all flip): Flips all the operator blocks in the expression.
After each move, you must quickly answer the updated value, which may be the same as the previous one. Remember that multiplication is performed before addition.
Input
The first line contains two integers () and (), where is the number of integers and is the number of queries.
The second line contains the initial expression of characters. Single digits from 1 to 9 are at odd positions, and operators + or * are at even positions.
Each of the next lines describes one move.
- The swap move is given as
s, with . If , no blocks are swapped. - The flip move is given as
f, with , meaning the th operator is flipped. - The all-flip move is given as
a, with no other parameters.
Output
On the first line, print the value of the initial expression. Then print more lines, one for each move, containing the value after that move. Print each value modulo .