#P16800. [蓝桥杯 2026 国 B] 方和质数

[蓝桥杯 2026 国 B] 方和质数

Problem Description

Xiao Lan is studying a special kind of prime number.

For a positive integer, add up all digits in its decimal representation to get its digit sum. If this positive integer satisfies both of the following conditions, Xiao Lan calls it a “square-sum prime”:

  • The number is a prime number.
  • The digit sum of the number is a perfect square.

For example, the digit sum of 1313 is 1+3=4=221 + 3 = 4 = 2^2, and 1313 is a prime number, so 1313 is a square-sum prime.

Now, given an interval [L,R][L, R] and a positive integer KK, find the KK-th square-sum prime in the interval [L,R][L, R] when sorted in increasing order. If there are fewer than KK square-sum primes in the interval, output −1-1.

Input Format

Input one line containing three integers L,R,KL, R, K.

Output Format

Output one line with one integer, representing the KK-th square-sum prime in the interval [L,R][L, R]. If it does not exist, output −1-1.

2 100 3
79
2 10 1
-1

Hint

Sample Explanation 1

The square-sum primes in the interval [2,100][2, 100] are 13,31,79,9713, 31, 79, 97 in order, so the 33-rd one is 7979.

Sample Explanation 2

The prime numbers in the interval [2,10][2, 10] are 2,3,5,72, 3, 5, 7. Their digit sums are 2,3,5,72, 3, 5, 7, none of which are perfect squares, so output −1-1.

Constraints

For 30%30\% of the testdata, 1≤L≤R≤1061 \le L \le R \le 10^6, 1≤K≤1041 \le K \le 10^4.

For all testdata, 1≤L≤R≤10121 \le L \le R \le 10^{12}, R−L+1≤106R - L + 1 \le 10^6, 1≤K≤1061 \le K \le 10^6.

Translated by ChatGPT 5