Superstitious Skylab Tower
Time limit1sMemory limit128 MB
Given a valid floor label with no digit 4 and no substring 13, count how many forbidden numbers precede it, then multiply by the floor height.
- Level
Medium7 of 10
- Topics
- Math, Dynamic programming
- Solved
- No attempts yet
Problem
By the 22nd century, structural engineering has advanced far enough to build orbital towers (space elevators) whose heights reach thousands of kilometers into space.
A team of scientists is planning one such tower as a research station that extends beyond geostationary orbit. Its floors are numbered sequentially starting from 0. Many of the scientists, however, are deeply superstitious: they refuse to use any floor whose number contains the digit 4 anywhere, or contains 13 as a substring. Numbers such as 4, 13, 14, 24, 40, 113, 130, and 413 are therefore all forbidden.
To keep every usable floor in service, the design team simply skips every forbidden number when it labels the floors. The labels run:
0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 15, 16, ...
(4 is skipped; 13 and 14 are skipped; and so on.)
Every floor is the same fixed height above the one directly below it, and the ground floor sits at height 0. Hence the floor at physical position (counting positions from 0 at ground level) stands at height . Note that a floor's label and its physical position differ once numbers start being skipped: the physical position of a label is how many valid labels come before it.
Given a floor's displayed label and the per-floor height, compute the exact height of that floor above the ground.
Input
The first line contains an integer , the number of test cases.
Each of the next lines contains two integers and :
- () — the displayed label of the floor whose height is requested.
- () — the height of each floor.
Every given label is guaranteed to be valid: it never contains the digit 4 or the substring 13.
Output
For each test case, print the height of the requested floor on its own line.