本文搬运自本人初中博客园博客,若图片加载不出来,可到原文查看:https://www.cnblogs.com/zhangtingxi/p/15600119.html

题目链接

想着T2,T3的题解都写了,就补一下T1的吧。

典型的筛法。

假如一个数含有7,则把它的倍数全筛走。

这里可以加一个小优化,假如这个数已经被筛过,就不需要再筛它的倍数了。

最后再倒着预处理每个数的下一个没被筛的是什么。

如果不预处理,不断6999999就可以卡死你。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<bits/stdc++.h>
using namespace std;
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();
}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();}return x*f;}
//#define M
//#define mo
#define N 10000050
int n, m, i, j, k;
int f[N], s[N], t, x;

int pan(int x)
{
while(x)
{
if(x%10==7) return 1;
x/=10;
}
return 0;
}

signed main()
{
// freopen("number.in", "r", stdin);
// freopen("number.out", "w", stdout);
for(i=1; i<=10000005; ++i)
{
if(pan(i))
{
if(!f[i])
for(j=i; j<=10000005; j+=i) f[j]=1;
}
// if(!f[i]) printf("%d\n", i);
}
for(i=10000005; i>=1; --i)
{
if(!f[i]) s[i]=i;
else s[i]=s[i+1];
// printf("s[%d]=%d\n", i, s[i]);
}
t=read();
while(t--)
{
x=read();
if(f[x]) printf("-1\n");
else printf("%d\n", s[x+1]);
}
return 0;
}