51NOD 1181 质数中的质数(质数筛法) 线性筛

思路:

在筛法的过程中,判断当前第i个素数x,看i是不是素数,看x是不是大于等于n。

代码:

#include<iostream>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define LL long long
#include<string.h>
#include<queue>
#include<stack>
#include<cstdio>
#include<cmath>
#include<map>
using namespace std;
const int maxn=10000000+6;
const int maxm=10000000;
#define mod 1000000007
#define INF 0x3f3f3f3f
int prime[maxm];
bool check[maxm];
bool is(int x)
{
    if(x<=1)
        return false;
    for(int a=2; a*a<=x; a++)
    {
        if(x%a==0)
            return false;
    }
    return true;
}
int cnt;
int ans;
int n;
void shai(int maxx)
{
    int tot=0;
    check[1]=1;
    for(int i=2; i<maxx; i++)
    {
        if(!check[i])
        {
            prime[tot++]=i;//tot<=i
            if(!check[tot]&&i>=n)
            {
                ans=i;
                return ;
            }
        }
        for(int j=0; j<tot&&i*prime[j]<maxx; j++)
        {
            check[i*prime[j]]=1;
            if(i%prime[j]==0)
                break;
        }
    }
}
int main()
{
    scanf("%d",&n);
    shai(2000002);
    printf("%d\n",ans);
    return 0;
}
发布了1062 篇原创文章 · 获赞 72 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/lanshan1111/article/details/104804547