解题关键:

根据质数的定义,在判断一个数n是否是质数时,我们只要用1至n-1去除n,看看能否整除即可。但我们有更好的办法。先找一个数m,使m的平方大于n,再用<=m的质数去除n(n即为被除数),如果都不能整除,则n必然是质数。如我们要判断1993是不是质数,50*50>1993,那么我们只要用1993除以<50的质数看是否能整除,若不能即为质数.

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,t;
int is_prime[],prime[];
int sieve(int n){
int p=;
fill(is_prime,is_prime+n+,);
is_prime[]=is_prime[]=;
for(int i=;i<=n;i++){
if(is_prime[i]){
prime[p++]=i;
for(int j=*i;j<=n;j+=i) is_prime[j]=;
}
}
return p;
}
int main(){
cin>>t;
int p=sieve(1e5);
while(t--){
bool flag=false;
cin>>n;
if(n==){
printf("yes\n");
continue;
}
for(int i=;prime[i]*prime[i]<=n;i++){
if(n%prime[i]==){
flag=true;
break;
}
}
if(flag) printf("no\n");
else printf("yes\n");
}
return ;
}

[51nod1106]质数检测的更多相关文章

  1. 51nod 1106 质数检测——Mr判素数

    质数检测一般都是根号n的写法 当然Mr判素数的方法可以实现log的复杂度2333 Mr判素数的话 我们根据费马小定理只要P是素数 那么另一个素数x 满足 x^P-1≡1(mod P) 同时 x^2%P ...

  2. 51nod 1186 质数检测 V2

    1186 质数检测 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 给出1个正整数N,检测N是否为质数.如果是,输出"Yes&quo ...

  3. (数论 欧拉筛法)51NOD 1106 质数检测

    给出N个正整数,检测每个数是否为质数.如果是,输出"Yes",否则输出"No".   Input 第1行:一个数N,表示正整数的数量.(1 <= N &l ...

  4. F - 质数检测 V2

    https://vjudge.net/contest/218366 Java解 import java.math.BigInteger; import java.util.Scanner; publi ...

  5. 51nod 1106 质数检测

    #include <bits/stdc++.h> using namespace std; int n; ; bool s[maxn]; void is_prime() { memset( ...

  6. 【51NOD-0】1106 质数检测

    [算法]数学 #include<cstdio> #include<cmath> bool ok(int x) { int m=(int)sqrt(x+0.5); ;i<= ...

  7. P1001 第K极值【tyvj】

    /*========================================== P1001 第K极值 内存限制 128MB 代码限制 64KB 描述 Description 给定一个长度为N ...

  8. go标准库的学习-crypto/rand

    参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/rand" rand包实现了用于加解密的更安全的随机数生成器. Var ...

  9. C语言1-100连加,求质数,算瑞年检测字母大小写,登录系统

    #include <stdio.h> void test(){//1+2+3+4+.....+100 int a,b; a=0; b=0; for ( ; a<=100; a++) ...

随机推荐

  1. 每天一个Linux命令(2)cd命令

    cd命令用来切换工作目录至dirname. 其中dirName表示法可为绝对路径或相对路径.若目录名称省略,则变换至使用者的home directory(也就是刚login时所在的目录).另外,~也表 ...

  2. 【leetcode刷题笔记】Max Points on a Line

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  3. php生成各种验证码

    片段 1 片段 2 片段 3 index.html ```<script type="text/javascript" src="jquery.min.js&quo ...

  4. table-cell笔记

    display:table-cell可将元素设为类似于table的td一样的布局,在垂直居中.两行自适应布局.等高布局下有很高的利用价值 详见: http://www.zhangxinxu.com/w ...

  5. 第二天----列表、深浅拷贝、元组、字符串、算数运算、字典、while

    列表 列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现. 基本操作: 索引切片追加删除长度切片循环包含 创建.查看列表: 列表中的数字不要加引号,列表的索引从0开始: lis ...

  6. Luogu-3705 [SDOI2017]新生舞会

    分数规划,最大费用最大流 题意可以简化为给出一个矩阵,要求每行和每列必须且只能取一个格子,要求\(sigma\ a_{i,j}/sigma\ b_{i,j}\) 最大 考虑分数规划,可以将式子转化: ...

  7. java访问控制---java编程语言

  8. python3字符串属性(二)

    1.S.isdecimal() -> bool    Return True if there are only decimal characters in S, False otherwise ...

  9. ZOJ 2724 Windows Message Queue (二叉堆,优先队列)

    思路:用优先队列 priority_queue,简单 两种方式改变队列 的优先级 (默认的是从大到小) #include<iostream> #include<queue> # ...

  10. Python习题-输出一个字符串中最长的子字符串及其长度

    描述:有个字符串$sd1#111$svda123!!!221&eSSDSDG,包含特殊字符.数字和字母,输出最长的子字符串和他的长度#例如上面的字符串包含数字字母的字符串是svda123,长度 ...