Metaprogramming
Time limit1sMemory limit1024 MB
Find the shortest program of add n, multiply n, and print commands that maps each given distinct input to its required output, or report that none exists.
- Level
Medium6 of 10
- Topics
- Number theory, Math, Brute force, Implementation
- Solved
- No attempts yet
Problem
Justas often takes part in programming contests. Because he spends so much time solving tasks, he wants to automate the process: he would like a tool that, given a task's tests, finds a program that solves it. Help Justas.
Justas gives you a list of tests, and you must find a program that handles all of them correctly. Each test consists of two integers — the test's input value and the expected result. All input values are distinct.
The programming language Justas uses is very simple. A program has a single variable that holds a non-negative integer of unbounded size. When the program starts, the test's input value is placed in this variable. The program is a list of commands:
add n— add to the variable ().multiply n— multiply the variable by ().print— output the value of the variable followed by a newline character.
For example, consider this program:
add 5
multiply 8
print
If the input value is , it prints ; if the input value is , it prints .
Justas does not want his solutions to exceed the time limit, so you must find a program with the fewest commands that correctly handles all of the given tests.
Input
The first line contains an integer — the number of tests. Each of the next lines contains two integers and — the input value and the required result of the -th test. All are distinct.
Output
Print a single integer — the number of commands in the shortest program that correctly handles every test (the print command is counted too). If no such program exists, print .