Given a positive integer, your job is writing a program to determine whether it is a prime number or not.

Input

There are several tests. Each test consists of a positive integer n(no more than 2^31) on a single line. Input is terminated by end of file.

Output

For each integer in the input, print a "YES" if it is a prime number, otherwise print a "NO" instead.

Sample Input

4
5
6

Sample Output

NO
YES
NO
【分析】
素性测试最基本的应用,主要精髓在于费马小定理和二次探测理论。
 /*
五代李煜
《清平乐·别来春半》
别来春半,触目柔肠断。砌下落梅如雪乱,拂了一身还满。
雁来音信无凭,路遥归梦难成。离恨恰如春草,更行更远还生。
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <iostream>
#include <string>
#include <ctime>
#define LOCAL
const int MAXN = + ;
using namespace std;
typedef long long ll;
ll n; ll pow(ll a, ll b, ll p){
if (b == ) return a % p;
ll tmp = pow(a, b / , p);
if (b % == ) return (tmp * tmp) % p;
else return (((tmp * tmp) % p) * (a % p)) % p;
}
//二次探测
bool Sec_Check(ll a, ll p){
ll tmp = pow(a, p, n);
if (tmp != && tmp != (n - )) return ;//不通过
if (tmp == (n - ) || (p % != )) return ;
return Sec_Check(a, p / );
}
bool miller_rabin(ll n){
ll cnt = ;
while (cnt--){
ll a = (rand()%(n - )) + ;
if (!Sec_Check(a, n - )) return ;
}
return ;
} int main(){
srand(time()); while (scanf("%lld", &n) != EOF){
if (n == ) printf("NO\n");
else if (miller_rabin(n)) printf("YES\n");
else printf("NO\n");
}
return ;
}

【HOJ1356】【Miller_rabin素性测试】Prime Judge的更多相关文章

  1. Miller_Rabin素性测试

    1. 为什么需要素性测试? 我们其实已经知道有一些判断素数的方法,比如: 遍历测试:待测试数n与2,3,...√n做除法判断余数是否为零,如果没有任何一个数可以整除n,则说明n为素数 Wilson定理 ...

  2. 优化后的二次测试Miller_Rabin素性测试算法

    ll random(ll n) { return (ll)((double)rand()/RAND_MAX*n + 0.5); } ll pow_mod(ll a,ll p,ll n) { ) ; l ...

  3. Miller_Rabin (米勒-拉宾) 素性测试

    之前一直对于这个神奇的素性判定方法感到痴迷而又没有时间去了解.借着学习<信息安全数学基础>将素性这一判定方法学习一遍. 首先证明一下费马小定理. 若p为素数,且gcd(a, p)=1, 则 ...

  4. POJ 1811 Prime Test 素性测试 分解素因子

    题意: 给你一个数n(n <= 2^54),判断n是不是素数,如果是输出Prime,否则输出n最小的素因子 解题思路: 自然数素性测试可以看看Matrix67的  素数与素性测试 素因子分解利用 ...

  5. 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case ...

  6. miller_rabin_素性测试

    摘自:http://blog.csdn.net/pi9nc/article/details/27209455 看了好久没看懂,最后在这篇博客中看明白了. 费马定理的应用,加上二次探测定理. Ferma ...

  7. Miller-Rabin素性测试|Pollard's Rho算法

    Miller-Rabin 素性测试 Miller-Rabin 素数测试 一本通上的M-R不透彻啊~ Miller-Rabin是利用随机化算法判断一个数是合数还是素数. 首先,如果一个数N是素数,那么他 ...

  8. 【算法杂谈】Miller-Rabin素性测试算法

    额,我们今天来讲一讲Miller-Rabin素性测试算法. 读者:怎么又是随机算法!!!(⊙o⊙)… [好了,言归正传] [费马小定理] 费马小定理只是个必要条件,符合费马小定理而非素数的数叫做Car ...

  9. Miller-Rabin 素性测试 与 Pollard Rho 大整数分解

    \(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...

随机推荐

  1. Linux Shell编程(10)——引用变量

    当要引用一个变量的值时,一般推荐使用双引号.使用双引号除了变量名前缀($).后引符(`)和转义符(\)外,会使shell不再解释引号中其它所有的特殊字符.用双引号时$仍被当成特殊字符,允许引用一个被双 ...

  2. (转载)MySQL关键字ORDER BY的使用

    例子: mysql), d_id ), name ), age ), sex ), homeaddr )); // 可以看到首先按照d_id进行升序排列,排列好了之后, // 若d_id字段中有相同的 ...

  3. (经常看看)jdk 设计模式

    在JDK(Java Development Kit)类库中,开发人员使用了大量设计模式,正因为如此,我们可以在不修改JDK源码的前提下开发出自己的应用软件,本文列出了部分JDK中的模式应用实例,有兴趣 ...

  4. C_FORCE_ROOT linux环境变量设置

    nano /etc/profile     gedit 也可以 结尾追加 export C_FORCE_ROOT="true"

  5. liststack——链表栈(procedure)

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include "list.h&q ...

  6. python实现应用程序在右键菜单中添加打开方式

    最近项目组开发的一个小工具想要在右键菜单中添加打开方式,以有道云笔记为例进行了需求拆解和代码编写 1.需求拆解: 如何实现手动添加右键菜单的打开方式: Step1:打开注册表编辑器,Win+R-> ...

  7. js生成随机数的方法实例总结

    js生成随机数主要用到了内置的Math对象的random()方法.用法如:Math.random().它返回的是一个 0 ~ 1 之间的随机数.有了这么一个方法,那生成任意随机数就好理解了.比如实际中 ...

  8. winform 曲线(贝塞尔) 分类: WinForm 2014-12-29 16:52 109人阅读 评论(0) 收藏

    <span style="font-size:14px;">//覆盖OnPaint事件</span> <span style="font-s ...

  9. 《Linux Device Drivers》第十章 中断处理——note

    概述:系统要及时的感知硬件的状态,通常有两种方式:一种是轮询.一种是通过响应硬件中断.前者会浪费处理器的时间,而后者不会. 准备并口 在没有节设定产生中断之前,并口是不会产生中断的 并口的标准规定设置 ...

  10. linux 同步备份 rsyncd 相关设置

    17:25 2013/10/18------------------ rsync linux 同步备份服务器 配置vi /etc/rsyncd.conf 配置文件 /usr/bin/rsync --d ...