Happy Numbers
Time limit0.5sMemory limit64 MB
Find the smallest start of K consecutive integers with exactly L numbers that are at most M or prime, or output -1.
- Level
Medium5 of 10
- Topics
- Number theory, Prefix sum, Sliding window
- Solved
- No attempts yet
Problem
Junhyung and Minhyung play a sequence game. Before the game starts, Minhyung picks three numbers , , and . Junhyung then has to say consecutive natural numbers.
Minhyung added one more rule to train Junhyung's mental arithmetic. Among the numbers Junhyung says, exactly of them must be happy numbers. A happy number is a number that meets at least one of the two conditions below.
- It is a natural number not greater than .
- It is a prime (a number with exactly 2 divisors).
Junhyung is slow at mental arithmetic, so he plans to work the answer out on a computer in secret. Write a program that answers Minhyung's questions.
Input
The first line contains the number of test cases ().
Each of the next lines contains , , and . (, )
Output
For each test case, print on one line the smallest of the numbers Junhyung says, that is, the starting value of the run of consecutive natural numbers. If several starting values satisfy the condition, print the smallest one. If no starting value satisfies the condition, or the smallest one is greater than 10,000,000, print -1.