C - Prime number or not

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

Your task is simple.Give you a number N, you should judge whether N is a prime number or not.

Input

There are multiple test cases. For each test case, there is an integer N(2<=N<=10^18).

Output

For each test case, you should output whether N is a prime number or not.If N is a prime number , you should output "It is a prime number."; otherwise you should output "It is not a prime number.";

Sample Input

2 4

Sample Output

It is a prime number. It is not a prime number.

这里关键的问题在于数据达到了1亿亿,没办法用普通的方法进行运算,所以这里用到了米勒拉宾大素数判定方法。

算法流程:根据费马小定理,a^(n-1)mod n==1,1<a<n,n为奇素数。随机在1~n的范围内取一个数a,进行式子的判定,返回1,就是伪素数,否则就是合数。因为伪素数是素数 的可能性为3/4,也就是正确率是1-1/4^k,所以我们要按定一个k使得正确率尽可能得大。所以要多次重复取随机数,然后判定。

文字代码:

1:重复MAX次运算

2:在1~n中取得随机数a

3:计算a^(n-1)mod n?=1,在这个计算里,注意到n可能很大,所以a^(n-1)可能越界,就想到用快速幂来边乘,边取模,但是又发现在n很大的时候,a*a都有可能溢出,所以想到了用快速幂的方法,进行快速积取模,边加边取模。这里的两个快速可避免溢出

4:在3中可得到的数如果为1,则在循环未结束前继续从2开始操作,否则直接返回0,表示n是合数

5:如果上面的循环能完整做完,说明n已经是强伪素数,我们可以返回1,判定为素数。

 #include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 10
__int64 muti(__int64 a,__int64 b,__int64 m)
{
__int64 ans=;
while(b)
{
if(b&)
ans=(ans+a)%m;
a=*a%m;
b/=;
}
return ans;
}
__int64 pow(__int64 a,__int64 b,__int64 m)
{
__int64 ans=;
while(b)
{
if(b&)
ans=muti(ans,a,m);
a=muti(a,a,m);//二进制。快速幂的思想
b/=;
}
return ans;
}
int miller_rabin(long long n)
{
__int64 i,a;
if(n==)
return ;
if(n<||!(n&))
return ;
srand((unsigned)time(NULL));
for(i=;i<=MAX;i++)
{
a=rand()%(n-)+;
if(pow(a,n-,n)!=)
return ;
}
return ;
}
int main()
{
__int64 n;
while(scanf("%I64d",&n)!=EOF)
if(miller_rabin(n))
printf("It is a prime number.\n");
else printf("It is not a prime number.\n");
return ;
}

FZU 1649 Prime number or not米勒拉宾大素数判定方法。的更多相关文章

  1. csu 1552(米勒拉宾素数测试+二分图匹配)

    1552: Friends Time Limit: 3 Sec  Memory Limit: 256 MBSubmit: 723  Solved: 198[Submit][Status][Web Bo ...

  2. HDU2138 & 米勒拉宾模板

    题意: 给出n个数,判断它是不是素数. SOL: 米勒拉宾裸题,思想方法略懂,并不能完全理解,所以实现只能靠背模板.... 好在不是很长... Code: /*==================== ...

  3. HDU 2138 How many prime numbers (判素数,米勒拉宾算法)

    题意:给定一个数,判断是不是素数. 析:由于数太多,并且太大了,所以以前的方法都不适合,要用米勒拉宾算法. 代码如下: #include <iostream> #include <c ...

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

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

  5. Miller_Rabin(米勒拉宾)素数测试

    2018-03-12 17:22:48 米勒-拉宾素性检验是一种素数判定法则,利用随机化算法判断一个数是合数还是可能是素数.卡内基梅隆大学的计算机系教授Gary Lee Miller首先提出了基于广义 ...

  6. C++米勒拉宾算法模板

    //我也忘了从哪找来的板子,不过对于2^63级的数据请考虑使用java内置的米勒拉宾算法. 1 #include <iostream> #include <string> #i ...

  7. POJ 1811Prime Test(米勒拉宾素数测试)

    直接套用模板,以后接着用 这里还有一个素因子分解的模板 #include <map> #include <set> #include <stack> #includ ...

  8. Miller_Rabin(米勒拉宾)素数测试算法

    首先需要知道两个定理: 1: 费马小定理: 假如p是素数,且gcd(a,p)=1,那么 a(p-1)≡1(mod p). 2:二次探测定理:如果p是素数,x是小于p的正整数,且,那么要么x=1,要么x ...

  9. Prime Time UVA - 10200(精度处理,素数判定)

    Problem Description Euler is a well-known matematician, and, among many other things, he discovered ...

随机推荐

  1. web前端开发教程系列-1 - 前端开发编辑器介绍

    目录: 前言 一. Webstorm 1. 优点 2. 缺点 3. 教程 4. 插件 5. 技巧 二. SublimeText 1. 优点 2. 缺点 3. 教程 4. 插件 5. 技巧 前言 由于很 ...

  2. 2016 版 Laravel 系列入门教程(五)【最适合中国人的 Laravel 教程】

    本教程示例代码见: https://github.com/johnlui/Learn-Laravel-5 在任何地方卡住,最快的办法就是去看示例代码. 本文是本系列教程的完结篇,我们将一起给 Arti ...

  3. Mobile Web

    Silun来给大家介绍几个常见的移动浏览器标签~ 当当当~ <meta name="apple-mobile-web-app-capable" content="y ...

  4. nginx root && alias 文件路径配置

    文章摘自:http://www.ttlsa.com/nginx/nginx-root_alias-file-path-configuration/ nginx指定文件路径有两种方式root和alias ...

  5. [转]不再以讹传讹,GET和POST的真正区别

    原文地址:http://www.nowamagic.net/librarys/veda/detail/1919 如果有人问你,GET和POST,有什么区别?你会如何回答? 我的经历 前几天有人问我这个 ...

  6. localStorage和sessionStorage的区别

    //在chrome测试的结果; 知识点1:localStorage和sessionStorage的区别; localStorage生命周期是永久,这意味着除非用户显示在浏览器提供的UI上清除local ...

  7. Java编程思想学习(十五) 注解

    注解Annotation又叫元数据,是JDK5中引入的一种以通用格式为程序提供配置信息的方式.使用注解Annotation可以使元数据写在程序源码中,使得代码看起来简洁,同时编译器也提供了对注解Ann ...

  8. php复习

    最近要用php,好久不用感觉手生.抓起<零基础学PHP>一书复习了下,顺带学了smarty模板语言,然后到慕课网看了些php中级视频教程,这里记录下. php最基本的文件上传 不用任何第三 ...

  9. BZOJ-1066 蜥蜴 最大流+拆点+超级源超级汇

    1066: [SCOI2007]蜥蜴 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 2582 Solved: 1272 [Submit][Status] ...

  10. 【poj3714】 Raid

    http://poj.org/problem?id=3714 (题目链接) 现在才搞平面最近点对..感觉有点尴尬 题意 给出平面上两组点,每组n个,求两组点之间最短距离 Solution1 平面最近点 ...