Reading Croatian Numbers
Time limit1sMemory limit128 MB
Given a letter and index N, find the N-th positive integer (up to 10^12) whose Croatian number reading starts with that letter, following specific rules about leading digits and place values.
- Level
Hard8 of 10
- Topics
- Math, Binary search, Simulation, Implementation
- Solved
- No attempts yet
Problem
The table below summarizes the first word used when reading positive integers in Croatian. Numbers are read in groups of three digits from the right. If the leading digit of the highest place is not 1, the reading begins with that digit's word. If it is 1, that 1 is usually omitted and the word for the place value is read first. The exceptions are the number 1 itself, which is read as jedan, and numbers whose highest three-digit group is from 11 to 19, which begin with the corresponding word in the table. For example, 40 begins with četiri, 705 with sedam, 150 with sto, 1 500 with tisuću, and 15 000 with petnaest.
For a given uppercase letter, list all positive integers whose Croatian reading starts with that letter in increasing order. For example, the sequence for P starts 5, 15, 50, 51, 52, ..., 59, 500, 501, ...
Find the N-th number in the sequence for the given letter.
Input
The input contains a letter L and an integer N separated by a space. L is one of D, J, M, O, P, S, and T.
The answer is guaranteed not to exceed 10^12.
Output
Print the N-th number in the sequence.