51nod 1106 质数检测——Mr判素数
质数检测一般都是根号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判素数的更多相关文章
- (数论 欧拉筛法)51NOD 1106 质数检测
给出N个正整数,检测每个数是否为质数.如果是,输出"Yes",否则输出"No". Input 第1行:一个数N,表示正整数的数量.(1 <= N &l ...
- 51nod 1106 质数检测
#include <bits/stdc++.h> using namespace std; int n; ; bool s[maxn]; void is_prime() { memset( ...
- 51nod 1186 质数检测 V2
1186 质数检测 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 给出1个正整数N,检测N是否为质数.如果是,输出"Yes&quo ...
- 【51NOD-0】1106 质数检测
[算法]数学 #include<cstdio> #include<cmath> bool ok(int x) { int m=(int)sqrt(x+0.5); ;i<= ...
- 51nod 1181 质数中的质数(质数筛法)
题目链接:51nod 1181 质数中的质数(质数筛法) #include<cstdio> #include<cmath> #include<cstring> #i ...
- UVA10200-Prime Time/HDU2161-Primes,例题讲解,牛逼的费马小定理和欧拉函数判素数。
10200 - Prime Time 此题极坑(本菜太弱),鉴定完毕,9遍过. 题意:很简单的求一个区间 ...
- miller_rabin算法检测生成大素数的RSA算法实现
import math from functools import reduce #用于合并字符 from os import urandom #系统随机的字符 import binascii # ...
- 【BZOJ-3667】Rabin_Miller算法 随机化判素数
3667: Rabin-Miller算法 Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 983 Solved: 302[Submit][Status ...
- 【Miller-Rabin随机判素数算法】
实用性介绍: #include<bits/stdc++.h> #define go(i,a,b) for(int i=a;i<=b;i++) #define T 5 #define ...
随机推荐
- IPReversePathFilter
nstat TcpExtIPReversePathFilter for i in /proc/sys/net/ipv4/conf/*/rp_filter ; do > echo 0 > $ ...
- PAT L1-044 稳赢
https://pintia.cn/problem-sets/994805046380707840/problems/994805086365007872 大家应该都会玩“锤子剪刀布”的游戏:两人同时 ...
- Java入门之:基本数据类型
Java基本数据类型 变量就是申请内存来存储值,也就是说,当创建变量的时候,需要在内存中申请空间.内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来存储该类型的数据,如下图所示: 因此, ...
- Apple - Hdu5160
Problem Description We are going to distribute apples to n children. Every child has his/her desired ...
- 转:learning to rank学习
learning to rank学习 转: http://blog.csdn.net/xuqianghit/article/details/8947819 1. 什么是learning to rank ...
- [WC2005]双面棋盘
description 洛谷 给出一个\(n\times n\)的黑白棋盘. \(m\)次操作,每次将一个格子进行颜色翻转,求每次操作后的黑白四连通块数. data range \[n\le 200, ...
- [洛谷P4999]烦人的数学作业
题目大意:定义$f(x)$表示$x$每一个数位(十进制)的数之和,求$\sum\limits_{i=l}^rf(i)$,多组询问. 题解:数位$DP$,可以求出每个数字的出现个数,再乘上每个数字的大小 ...
- POJ3621:Sightseeing Cows——题解
http://poj.org/problem?id=3621 全文翻译参自洛谷:https://www.luogu.org/problemnew/show/P2868 题目大意:一个有向图,每个点都有 ...
- BZOJ2434:[NOI2011]阿狸的打字机——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=2434 https://www.luogu.org/problemnew/show/P2414 打字 ...
- [Leetcode] Construct binary tree from preorder and inorder travesal 利用前序和中续遍历构造二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume tha ...