HLJOJ1171(大素数判定)

多组测试数据,每组测试数据第一行输入一个正整数n(0< n < 10^9)。

每组测试数据,若n为素数,那么输出“YES”,否则输出“NO”。(引号不输出)


思路:由于n非常的大,打表nlogn也不行,我们换一种思路,我们不打10^9,我们只打出它的开平方10^5就差不多了,找出小于10^5以内的素数,然后我们判定n的时候再用

普通素数判别法判定就行


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
using namespace std;
#define LL long long
const double eps = 1e-6;
const int INF = 10000000;
int n, m , k;
 
const int maxn = 60001;
 
bool ispri[maxn];
 
int pri[maxn];
 
void is()
{
    int cnt = 0;
    memset(ispri , true , sizeof(ispri));
    ispri[1] = 0;
    for(LL i  = 2 ; i < maxn ; i ++)
    {
        if(ispri[i])
        {
            pri[++ cnt] = i;
            for(LL j = i * 2 ; j < maxn ; j += i)
                ispri[j] = false;
        }
    }
}
 
bool isp(int n)
{
    int m = sqrt(n);
    for(int i = 1 ; pri[i] <= m ; i ++)
        if(n % pri[i] == 0)
            return false;
    return true;
}
 
int main()
{
 
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n;
    is();
    while(~scanf("%d",&n))
    {
        if(n == 0 || n == 1)
        {
           printf("NO\n");
            continue;
        }
 
        if(isp(n))
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
 
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值