luogu#P7960. [NOIP2021] 报数

    ID: 7302 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>2021NOIP 提高组O2优化素数判断,质数,筛法

[NOIP2021] 报数

Problem Description

The counting-off game is a widely known casual game. Everyone in the game takes turns counting in a fixed order. However, if the next number to be spoken is a multiple of 77, or its decimal representation contains the digit 77, then this number must be skipped; otherwise, the player loses.

On a sunny afternoon, little r and little z, who had just finished the SPC20nn contest, were bored and started playing this counting-off game. But with only two players, it was still easy to compute, so they played for a long time without deciding a winner. Then little z had an idea and decided to strengthen the game: for any number whose decimal representation contains the digit 77, none of its multiples are allowed to be spoken.

Formally, let p(x)p(x) indicate whether the decimal representation of xx contains the digit 77. If it does, then p(x)=1p(x) = 1; otherwise, p(x)=0p(x) = 0. Then a positive integer xx cannot be spoken if and only if there exist positive integers yy and zz such that x=yzx = yz and p(y)=1p(y) = 1.

For example, if little r says 66, then since 77 cannot be spoken, little z needs to say 88 next. If little r says 3333, then since 34=17×234 = 17 \times 2 and 35=7×535 = 7 \times 5 both cannot be spoken, little z needs to say 3636 next. If little r says 6969, then since all numbers from 707970 \sim 79 contain 77, little z must say 8080 next.

Now little r previously said xx, and little z wants to quickly compute what number he should say next. But he soon finds that this game is much harder to compute than the original one, so he needs your help. Of course, if the number xx said by little r itself is not allowed, you should also quickly realize that little r has lost.

Since little r and little z have been playing for a long time, you also need to answer many questions from little z.

Input Format

The first line contains a positive integer TT, indicating the number of queries from little z.

The next TT lines each contain a positive integer xx, indicating the number said by little r in this round.

Output Format

Output TT lines in total. For each line, output an integer: if the number said by little r in this round is not allowed, output 1-1; otherwise, output the number that little z should say next.

4
6
33
69
300

8
36
80
-1

5
90
99
106
114
169

92
100
109
-1
180

见附件中的 number/number3.in
见附件中的 number/number3.ans
见附件中的 number/number4.in
见附件中的 number/number4.ans

Hint

Sample Explanation #1

The first 33 queries in this sample have already been explained in the problem description.

For the 44-th query, since 300=75×4300 = 75 \times 4, and 7575 contains the digit 77, little r loses immediately.

Constraints

For 10%10\% of the testdata, T10T \leq 10, x100x \leq 100.
For 30%30\% of the testdata, T100T \leq 100, x1000x \leq 1000.
For 50%50\% of the testdata, T1000T \leq 1000, x10000x \leq 10000.
For 70%70\% of the testdata, T10000T \leq 10000, x2×105x \leq 2 \times {10}^5.
For 100%100\% of the testdata, 1T2×1051 \le T \leq 2 \times {10}^5, 1x1071 \le x \leq {10}^7.

Translated by ChatGPT 5