Compressed Formula
Time limit2sMemory limit512 MB
Evaluate an arithmetic formula with +, -, * over non-negative integers, given as N runs where each short string repeats r_i times, and print the result modulo 1,000,000,007.
- Level
Medium7 of 10
- Topics
- Math, String, Implementation, Divide and conquer
- Solved
- No attempts yet
Problem
You are given a long but simple formula in a compressed format. A compressed formula is a sequence of pairs of an integer and a string , where each consists only of the digits 0 to 9 and the characters +, -, *. To restore the original formula, repeat exactly times for every , then concatenate the resulting strings in the order of the sequence.
A restored formula always satisfies the following BNF.
<expression> := <term> | <expression> '+' <term> | <expression> '-' <term>
<term> := <number> | <term> '*' <number>
<number> := <digit> | <non-zero-digit> <number>
<digit> := '0' | <non-zero-digit>
<non-zero-digit> := '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
Here + means addition, - means subtraction, and * means multiplication of integers.
Write a program that computes the value of the given formula modulo 1,000,000,007. Here modulo is the non-negative integer such that some integer satisfies and . For integers and , that is uniquely determined.
Input
The input consists of a single test case.
N
r1 s1
...
rN sN
The first line contains the length () of the compressed formula. Each of the following lines holds one piece of the compressed formula. The -th line contains an integer () and a string (), where is the number of repetitions of and is a piece of the original formula. The formula restored by repeating and concatenating the pieces satisfies the BNF above.
Output
Print the value of the given compressed formula modulo 1,000,000,007.