ASM
Time limit1sMemory limit1024 MB
Find the fewest add/multiply/print commands in a one-variable program whose printed concatenation matches every test's required output.
- Level
Hard8 of 10
- Topics
- Brute force, Dynamic programming, String, Math
- Solved
- No attempts yet
Problem
Justas takes part in programming olympiads. Because solving tasks takes a lot of time, he wants a program that, given a task's tests, automatically finds a solution. Help him!
You are given a list of tests and must find a single program that solves all of them. Each test is a pair of numbers: an initial number and a result. The initial numbers of the tests are all distinct.
The programs are written in a very simple language with a single variable that holds a non-negative integer of arbitrary size (). When a program starts, the test's initial number is stored in . A program is a list of commands:
add n— add to ()multiply n— multiply by ()print— output the current value of , written in decimal with no leading zeros (except that a value of is printed as0). The value is printed with no separators and no newline.
For one test, the program's output is the concatenation of everything its print commands write. For example, the program
multiply 2
print
add 5
print
started with the initial number outputs 27, and started with the initial number outputs 1217.
Among all programs that produce the correct output for every test, Justas wants one with the fewest commands. Report that minimum number of commands.
Input
The first line contains an integer — the number of tests. Each of the next lines contains two integers and : is the initial number of the -th test and is the output that must be produced. All are distinct.
Output
Print a single integer — the minimum possible number of commands in a program that produces the correct output for every test. If no such program exists, print -1.