Skip to Content

Plus or times

Level A. Teaches functions, reading a Char and match.

A number x starts at 1. You are given q operations (1 ≤ q ≤ 2·10⁵), each + y or * y with 0 ≤ y ≤ 10⁹. Apply them in order, replacing x by x + y or x · y, and print the final x modulo 10⁹ + 7.

Input: q, then q lines op y. Output: one number.

InputOutput
3
+ 4
* 3
+ 1
16
4 tests · Ctrl+Enter
Hints
  • apply is a function with typed parameters and a return type. Its body is one match, and a match is an expression, so its value is the function's result.
  • let (op, y): (Char, 0..=1e9) = read() reads a character and a number in one line. The type is the input format.
  • '+' is a pattern; _ matches anything else. Take the remainder after every step: x · y reaches 10¹⁸, which still fits in a 64-bit Int, but a product of two of those would not.
  • Functions and records covers fn, match and reading tuples; the types chapter explains why the remainder keeps the product in 64 bits.