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. 05.C#泛型类型(三章3.1-3.2)

    接下来说下C#中的泛型,熟练地使用泛型能提高代码的重用性,使用我们代码瞬间就高大上了,当然只有一点点,真的只有一点点,因为后面要学习和掌握的知识还有很多.先来看下一个使用Dictionary<T ...

  2. WCF 入门(19)

    前言 天气转凉,提前过冬了.感冒依旧没好,因为双休日伙食太好了,各种鱼各种肉. 第19集 创建然后抛出强类型的SOAP faults  Creating and throwing strongly t ...

  3. javascript基础知识拾遗

    1 下面列出的值被当作假 false null undefined '' 0 NaN 其它所有值被当作是真 console.log(undefined || true); //true console ...

  4. 每天一个linux命令(27):ln 命令

    ln 是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在不同的目录,用到相同的文件时,我们不需要在 每一个需要的目录下都放一个必须相同的文件,我们只 ...

  5. SP*

    1.PS1——默认提示符 root@tcx2250-14:/etc# echo $PS1\u@\h:\w\$ \u是用户名 \h是主机名 \w是当前目录的完整路径.请注意当你在主目录下的时候,如上面所 ...

  6. Java获取各种常用时间方法大全

    Java获取各种常用时间方法大全 package cc.javaweb.test; Java中文网,Java获取各种时间大全 import java.text.DateFormat; import j ...

  7. maven加载spring包

    <dependencies> <dependency> <groupId>org.springframework</groupId> <artif ...

  8. Longest Increasing Common Subsequence (LICS)

    最长上升公共子序列(Longest Increasing Common Subsequence,LICS)也是经典DP问题,是LCS与LIS的混合. Problem 求数列 a[1..n], b[1. ...

  9. 北航 编译实践 PL/0文法

    编译实践-PL\0编译系统实现 姓名:   专业: 计算机科学与技术 学院: 软件学院 提交时间: 2013年12月25日 北京航空航天大学·软件学院 编译实践-PL\0编译系统实现 实验要求 以个人 ...

  10. android 自定义控件 使用declare-styleable进行配置属性(源码角度)

          android自定义styleableattrs源码 最近在模仿今日头条,发现它的很多属性都是通过自定义控件并设定相关的配置属性进行配置,于是便查询了解了下declare-styleabl ...