#P10404. 「XSOI-R1」原神数

    ID: 11705 远端评测题 1500ms 128MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>洛谷原创O2优化素数判断,质数,筛法

「XSOI-R1」原神数

Problem Description

A Genshin number xx must satisfy both of the following conditions:

  • xx is a prime number.
  • All decimal digits of xx are pairwise distinct.

For example, 131131 is not a Genshin number because its hundreds digit equals its ones digit; 4242 is not because it is not prime; 1717 is a Genshin number.

There are qq queries. You need to find the number of Genshin numbers between ll and rr (inclusive).

Input Format

The first line contains a positive integer qq.

The next qq lines each contain two positive integers ll and rr.

Output Format

Output qq lines. Each line contains one positive integer, which is the answer to the corresponding query.

4
45 66
100 140
2 90
114514 1919810
4
6
23
13321

Hint

Sample Explanation #1

Between 100100 and 140140, the Genshin numbers are 103,107,109,127,137,139103,107,109,127,137,139, a total of 66.

Constraints and Notes

This problem uses bundled tests.

  • Subtask 0 (15 pts): 1lr1021 \le l \le r \le 10^2.
  • Subtask 1 (15 pts): 1lr1041 \le l \le r \le 10^4.
  • Subtask 2 (35 pts): 1lr1071 \le l \le r \le 10^7.
  • Subtask 3 (35 pts): 1lr10181 \le l \le r \le 10^{18}.

For all testdata, it is guaranteed that 1lr10181 \leq l \leq r \leq 10^{18} and 1q1051 \leq q \leq 10^5.

Translated by ChatGPT 5