A Simple Sequence of Digits
Time limit1sMemory limit512 MB
Given n and k, build the digit string formed by concatenating the first n primes, then delete exactly k digits to leave the largest possible number.
- Level
Medium5 of 10
- Topics
- Greedy, Stack, Number theory, Implementation
- Solved
- No attempts yet
Problem
During the break before math class, Roma decided to practice deciding whether a number is prime. Recall that a prime is a natural number with exactly two distinct natural divisors: one and itself. First he wrote the first prime on the board, then appended the second prime to its right, then the third, and so on. In total Roma wrote the first primes on the board. His work left a single long number on the board, which begins as <<23571113171923\dots>>.
When Yelena Yevgenyevna, Roma's teacher, entered the classroom, she gave the class the following task: cross out digits from the number on the board so that the remaining number is as large as possible.
Help Roma and his classmates solve the task, so that the strict teacher does not give them a failing grade.
Input
The input file for this problem contains several test cases. The first line of the input file contains the number , the number of test cases in the file.
The next lines describe the test cases, each consisting of two positive integers and . It is guaranteed that the first primes contain at least digits in total.
The sum of all in the input file does not exceed 400000.
Output
For each test case, output the required maximum number for the corresponding and on a separate line.
Hint
In the first test, Roma wrote the number 2357. The largest number that can be obtained by crossing out two digits from it is 57.
In the second test, Roma wrote the number 235711. The largest number that can be obtained by crossing out three digits from it is 711.