题目:
代码:
#include<bits/stdc++.h>
#include<queue>
using namespace std;
const int maxn=1e6+30;
const __int64 inf=(__int64)1<<62;
int n,m;
int p[maxn>>2];//存储素数
bool a[maxn];
//O(n) 素数筛
void init() {
memset(a,false,sizeof(a));//初始全部为素数
int num=0;
for(int i=2;i<maxn;++i) {
if(!a[i]) p[num++]=i;
for(int j=0;(j<num&&i*p[j]<maxn);++j) {
a[i*p[j]]=1;
if(i%p[j] == 0) break;
}
}
}
int main() {
init();
while(cin>>n) {
int t=n;
n+=1;
for(int m=1; m<=1000; ++m,n+=t) {
if(a[n]) {
cout<<m<<endl;
break;
}
}
}
return 0;
}