【NOIP2021 报数】题解

题目链接

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

典型的筛法。

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

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

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

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

Code

#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;
}

posted @ 2021-11-24 21:10  zhangtingxi  阅读(356)  评论(0编辑  收藏  举报