为什么会T 玄关
查看原帖
为什么会T 玄关
1190741
Violet_Evergardon楼主2024/11/10 17:14

筛法

#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=1e7+5;
int no[N];
bool is7(int x){
	if(x%10==7) return true;
	else if(x>10)
		return is7(x/10);
	else return false;
}
int t,x;
int nxt[N];
signed main(){
//	freopen("number.in","r",stdin);
//	freopen("number.out","w",stdout);
	scanf("%d",&t);
	for(int i=1;i<N;i++){
		if(is7(i)||i%7==0){
			for(int j=1;j*i<N;j++){
				no[i*j]=1;
			}
		}
	}
	int a=1;
	for(int i=2;i<=1e7+50;i++){
		if(!no[i]){
			nxt[a]=i;
			a=i;
		}
	}
	for(int i=1;i<=t;i++){
		cin>>x;
		if(no[x]) cout<<-1<<endl;
		else	cout<<nxt[x]<<endl;
	}
	return 0;
}
2024/11/10 17:14
加载中...