思路其实很简单,就是类似于埃式筛,将1e7内的符合条件的数全部筛出来即可,同时记录一个nxt数组即可
Code
#include<bits/stdc++.h>
#define re register
#define ll long long
#define inl inline
using namespace std;
int read(){
int sum=0,f=1;char c=getchar();
while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
while(isdigit(c)){sum=(sum<<3)+(sum<<1)+(c^48);c=getchar();}
return sum*f;
}
const int N=1e7+1000;
bool f[N];
int nxt[N];
inl bool pan(int x){
while(x){
if(x%10==7) return 1;
x/=10;
}
return 0;
}
inl void init(){
int tmp=0;
for(re int i=1;i<=N-100;i++){
if(f[i]) continue;
if(pan(i)){
for(re int j=i;j<=N-100;j+=i){
f[j]=1;
}
continue;
}
nxt[tmp]=i;
tmp=i;
}
}
int main(){
init();
int T=read();
while(T--){
int x=read();
if(f[x]){
puts("-1");
}
else printf("%d\n",nxt[x]);
}
return 0;
}