#P7588. 双重素数(2021 CoE-II A)

    ID: 6376 远端评测题 1000ms 96~128MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>数学二分2021O2优化素数判断,质数,筛法

双重素数(2021 CoE-II A)

Description

A prime number is a natural number greater than 11 that has no divisors other than 11 and itself. A double prime is defined as a prime number whose sum of digits is also a prime number. Given a closed interval, determine how many double primes are in this interval.

Input Format

The input contains multiple sets of testdata.

The first line contains an integer TT, representing the number of test cases. Each of the next lines contains one test case, with two integers LL and RR separated by a space.

Output Format

For each test case, output one line containing an integer, representing the number of double primes in the closed interval [L, R][L,\ R].

4
3 3
4 4
1 5
1 15
1
0
3
5

Hint

Sample explanation.

From 11 to 1515, there are 66 prime numbers: 22, 33, 55, 77, 1111, 1313. For the first five primes, the sum of digits is also prime, so they are all double primes. The sum of digits of the prime 1313 is 44, which is not prime, so 1313 is not a double prime.


Constraints.

  • Subtask 11: 1LR1021 \le L \le R \le 10^2, 1010 points.
  • Subtask 22: 1LR1041 \le L \le R \le 10^4, 2020 points.
  • Subtask 33: 1LR1061 \le L \le R \le 10^6, 6060 points.
  • Subtask 44: 1LR1081 \le L \le R \le 10^8, 1010 points.

For 100%100\% of the data, 1T1001 \le T \le 100.


Hint (the data has been strengthened).

The last subtask requires your program to have high space efficiency and time efficiency, otherwise it is easy to exceed the memory limit or time limit.

Translated by ChatGPT 5