[51nod1106]质数检测
解题关键:
根据质数的定义,在判断一个数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]质数检测的更多相关文章
- 51nod 1106 质数检测——Mr判素数
质数检测一般都是根号n的写法 当然Mr判素数的方法可以实现log的复杂度2333 Mr判素数的话 我们根据费马小定理只要P是素数 那么另一个素数x 满足 x^P-1≡1(mod P) 同时 x^2%P ...
- 51nod 1186 质数检测 V2
1186 质数检测 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 给出1个正整数N,检测N是否为质数.如果是,输出"Yes&quo ...
- (数论 欧拉筛法)51NOD 1106 质数检测
给出N个正整数,检测每个数是否为质数.如果是,输出"Yes",否则输出"No". Input 第1行:一个数N,表示正整数的数量.(1 <= N &l ...
- F - 质数检测 V2
https://vjudge.net/contest/218366 Java解 import java.math.BigInteger; import java.util.Scanner; publi ...
- 51nod 1106 质数检测
#include <bits/stdc++.h> using namespace std; int n; ; bool s[maxn]; void is_prime() { memset( ...
- 【51NOD-0】1106 质数检测
[算法]数学 #include<cstdio> #include<cmath> bool ok(int x) { int m=(int)sqrt(x+0.5); ;i<= ...
- P1001 第K极值【tyvj】
/*========================================== P1001 第K极值 内存限制 128MB 代码限制 64KB 描述 Description 给定一个长度为N ...
- go标准库的学习-crypto/rand
参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/rand" rand包实现了用于加解密的更安全的随机数生成器. Var ...
- C语言1-100连加,求质数,算瑞年检测字母大小写,登录系统
#include <stdio.h> void test(){//1+2+3+4+.....+100 int a,b; a=0; b=0; for ( ; a<=100; a++) ...
随机推荐
- Python基础(2)_数字和字符串类型
一.数据类型 1.数字 整型 Python的整型相当于C中的long型,Python中的整数可以用十进制,八进制,十六进制表示. >>> --------->默认十进制 > ...
- 回忆基础:制作plist文件
-(void)creatPlistFileWithArr:(NSArray *)array{ //将字典保存到document文件->获取appdocument路径 NSString *docP ...
- dev系列之gridview
gridview新增一行就激活编辑,及显示闪动的光标 gridView1.ShowEditor(); 隐藏Gridview表头上面的panel this.gridView1.OptionsView.S ...
- mysql管理工具之pt-heartbeat
之前我一直用Seconds_behind_master来衡量主从的延迟,今天看到文档,才觉得多么不可靠!以下是官方文档的描述: In essence, this field measures the ...
- 纯CSS3垂直动画菜单
在线演示 本地下载
- read + 计算
#!/bin/sbin read -p "please input first number:" a read -p "please input second numbe ...
- ML三(人工神经网络)
人工神经网络 Artificial Neural Nerworks 基本术语概念: 人工神经网络(Artificial Neural Networks,ANN) 感知器(Perceptron):以一个 ...
- .net短信接口调用示例(106短信通道)
1. [代码]调用代理示例 using System;using System.Data;using System.Configuration;using System.Collections;usi ...
- 理解javascript this 值
如何确定this的值 this值会被传递给所有函数,this的值是基于运行时调用函数的上下文. 例如:从全局作用域调用sayFoo函数时,this引用window对象 当它作为myObject的一种方 ...
- jmeter--简单使用
1.启动jmeter 2.创建线程组 2.点击线程组,选择添加,选择sampler(采样器),选择http请求 3.在添加的请求页面中,填写服务器名称或IP,端口,路径,请求的方法 4.添加请求的参数 ...