质数检测一般都是根号n的写法 当然Mr判素数的方法可以实现log的复杂度2333

Mr判素数的话 我们根据费马小定理只要P是素数 那么另一个素数x 满足 x^P-1≡1(mod P)

同时 x^2%P==1 的解只有 x==1||x==P-1 可以利用这第二个式子做二次探测

利用 2 3 5 7 11 13 17 这七个素数可以保证int内正确性QAQ

当然记得判断2 3 5 7 11 13 17  因为费马小定理成立的条件是 x和P 互质

#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
int T,n;
int num[]={,,,,,,};
LL pmod(LL a,LL b,LL c){
LL ans=;
while(b){
if(b&) ans=ans*a%c;
b>>=; a=a*a%c;
}
return ans;
}
bool calc(LL x,LL P){
LL ly=P-,yy,last;
while(ly%==) ly/=;
last=yy=pmod(x,ly,P);
while(ly!=P-){
yy=yy*yy%P;
if(yy==&&last!=&&last!=P-) return ;
ly*=; last=yy;
}
return yy==;
}
bool pd(LL n){
if(n==||n==||n==||n==||n==||n==||n==) return ;
for(int i=;i<;i++)if(!calc(num[i],n)) return ;
return ;
}
int main(){
T=read();
while(T--){
n=read();
if(pd(n)) printf("Yes\n");
else printf("No\n");
}
return ;
}

51nod 1106 质数检测——Mr判素数的更多相关文章

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

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

  2. 51nod 1106 质数检测

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

  3. 51nod 1186 质数检测 V2

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

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

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

  5. 51nod 1181 质数中的质数(质数筛法)

    题目链接:51nod 1181 质数中的质数(质数筛法) #include<cstdio> #include<cmath> #include<cstring> #i ...

  6. UVA10200-Prime Time/HDU2161-Primes,例题讲解,牛逼的费马小定理和欧拉函数判素数。

                                                    10200 - Prime Time 此题极坑(本菜太弱),鉴定完毕,9遍过. 题意:很简单的求一个区间 ...

  7. miller_rabin算法检测生成大素数的RSA算法实现

      import math from functools import reduce #用于合并字符 from os import urandom #系统随机的字符 import binascii # ...

  8. 【BZOJ-3667】Rabin_Miller算法 随机化判素数

    3667: Rabin-Miller算法 Time Limit: 60 Sec  Memory Limit: 512 MBSubmit: 983  Solved: 302[Submit][Status ...

  9. 【Miller-Rabin随机判素数算法】

    实用性介绍: #include<bits/stdc++.h> #define go(i,a,b) for(int i=a;i<=b;i++) #define T 5 #define ...

随机推荐

  1. iOS- 给App添加内购& 验证购买iOS7新特性

    1.内购——应用内购买 我所说的内购——也可以说是应用内购买 大家都知道通过苹果应用程序商店有三种主要赚钱的方式: 1.直接收费(与国内大部分用户的消费习惯相悖,如果要收费,直接收高的,别收6块钱) ...

  2. PAT 1058 选择题

    https://pintia.cn/problem-sets/994805260223102976/problems/994805270356541440 批改多选题是比较麻烦的事情,本题就请你写个程 ...

  3. open-stf 安装篇(linux)

       OpenSTF 百度MTC的远程真机调试 Testin的云真机 腾讯WeTest的云真机 阿里MQC的远程真机租用 什么是OpenSTF? OpenSTF是一个手机设备管理平台,可以对手机进行远 ...

  4. CentOS修改DNS、IP地址、网关

    一.CentOS 修改DNS 修改对应网卡的DNS的配置文件 # vi /etc/resolv.conf 修改以下内容 nameserver 8.8.8.8 #google域名服务器 nameserv ...

  5. 使用图片方式显示email地址

    import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics2D ...

  6. 【转】how can i build fast

    http://blog.csdn.net/pcliuguangtao/article/details/5830860

  7. Maven jeetsite项目 搭建

    , 一直没有系统的总结一下Maven的知识,今天,想从网上找一个Maven的项目,练练手,顺便学习一下maven的原理 和布局. 官网:http://www.jeesite.com/ 没想到,上来就给 ...

  8. codeforces 730 j.bottles

    J. Bottles time limit per test 2 seconds memory limit per test 512 megabytes input standard input ou ...

  9. HDU 4869 Turn the pokers(思维+逆元)

    考试的时候没有做出来... 想到了答案一定是一段连续的区间,一直在纠结BFS判断最后的可行1数. 原来直接模拟一遍就可以算出来最后的端点... 剩下的就是组合数取模了,用逆元就行了... # incl ...

  10. P3074 [USACO13FEB]牛奶调度Milk Scheduling

    题目描述 Farmer John's N cows (1 <= N <= 10,000) are conveniently numbered 1..N. Each cow i takes ...