**链接:****传送门 **

题意:题目给出费马小定理:Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). 我们知道Miller-Rabin素数测试的算法原理就是基于费马小定理的,因为我们在测试底数的时候只是随机一些 a ,所以可能有的合数就脸一白通过了测试,于是就产生了伪素数这一概念,现在给你一对 p and a,判断 p 是否是以 a 为基的伪素数

思路:****对于素数来说是不存在伪素数这一说的,只有合数才可能出现伪素数,所以当 p 为合数 且满足式子:ap = a (mod p).则 p 是以 a 为基的伪素数


/*************************************************************************
> File Name: poj3641.cpp
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年05月22日 星期一 14时56分11秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std; #define TIME 1000 // 米勒测试次数上限
#define ll long long ll RDM(ll n){ // 生成随机底数随机底数范围在[ 1 , n ]
return (ll)( (double)rand()/RAND_MAX * n + 0.5 );
}
ll quick_pow(ll a,ll x,ll m){ // O(logx)
ll ret = 1;
while(x){
if( x&1 ) ret = ( (ret%m) * (a%m) ) % m;
a = ( (a%m) * (a%m) ) % m;
x >>= 1;
}
return ret;
}
bool Mille_Rabin(ll n){ // 复杂度大约为O(TIME) PS:除非脸黑成功避过TIME次测试
for(int i = 1 ; i <= TIME ; i++){
ll a = RDM(n-2) + 1; // 随机底数
if( quick_pow(a,n-1,n) != 1 )
return false;
}
return true;
}
bool Pseudo_Prime(ll p,ll a){
return quick_pow(a,p,p) == a ? true : false;
}
int main(){
ll p , a;
while(~scanf("%lld%lld",&p,&a)){
if( p == 0 && a == 0 ) break; if( Mille_Rabin(p) ){
printf("no\n");
}
else{
if( Pseudo_Prime(p,a) ) printf("yes\n");
else printf("no\n");
}
}
return 0;
}

POJ Pseudoprime numbers( Miller-Rabin素数测试 )的更多相关文章

  1. POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】

    Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...

  2. HDU1164_Eddy&#39;s research I【Miller Rabin素数测试】【Pollar Rho整数分解】

    Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  3. POJ2429_GCD &amp; LCM Inverse【Miller Rabin素数測试】【Pollar Rho整数分解】

    GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 ...

  4. POJ1811_Prime Test【Miller Rabin素数測试】【Pollar Rho整数分解】

    Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...

  5. Miller Rabin素数检测与Pollard Rho算法

    一些前置知识可以看一下我的联赛前数学知识 如何判断一个数是否为质数 方法一:试除法 扫描\(2\sim \sqrt{n}\)之间的所有整数,依次检查它们能否整除\(n\),若都不能整除,则\(n\)是 ...

  6. poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题

    Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...

  7. POJ3641 Pseudoprime numbers(快速幂+素数判断)

    POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...

  8. poj 1811 Prime Test 大数素数测试+大数因子分解

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 27129   Accepted: 6713 Case ...

  9. Miller Rabin素数检测

    #include<iostream> #include<cstdio> #include<queue> #include<cstring> #inclu ...

随机推荐

  1. Python:利用 selenium 库抓取动态网页示例

    前言 在抓取常规的静态网页时,我们直接请求对应的 url 就可以获取到完整的 HTML 页面,但是对于动态页面,网页显示的内容往往是通过 ajax 动态去生成的,所以如果是用 urllib.reque ...

  2. vim 插件配置博客记录

    本来打算自己写下各种经常使用vim的插件安装方法, 可是搜索了下, 发现别人都写过了, 在写一遍也没有意思, 特此记录. Vim 经常使用命令 http://blog.csdn.net/hittata ...

  3. Codeforces444A_DZY Loves Physics

    DZY Loves Physics time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. win7 32位支持多大内存|win7 32位旗舰版最多能识别多少内存

    win7 32位支持多大内存|win7 32位旗舰版最多能识别多少内存 内存的大小决定系统运行速度,所以不少人认为只要内存加大就行了,其实这是不对的,因为win7 32位能支持的内存大小是有限制的,并 ...

  5. Softmax回归——logistic回归模型在多分类问题上的推广

    Softmax回归 Contents [hide] 1 简介 2 代价函数 3 Softmax回归模型参数化的特点 4 权重衰减 5 Softmax回归与Logistic 回归的关系 6 Softma ...

  6. (Go)01.Windows 安装 Go语言开发环境以及使用

    一.Go语言下载 go语言官方下载地址:https://golang.org/dl/ 找到适合你系统的版本下载,本人下载的是windows msi版本.也可以下载Source自己更深层次研究go语言 ...

  7. 抽象类(abrstract class)与接口(interface)有何异同

    抽象类:如果一个类中包含抽象方法(用abstract修饰的方法),那么这个类就是抽象类 接口:是指一个方法的集合,接口中的所有方法都没有方法体 相同点: 1)都不能被实例化 2)接口的实现类或抽象类的 ...

  8. Eigen3

    Eigen用源码的方式提供给用户使用,在使用时只需要包含Eigen的头文件即可进行使用. Eigen: C++开源矩阵计算工具——Eigen的简单用法 http://blog.csdn.net/aug ...

  9. F - Dima and Lisa(哥德巴赫猜想)

    Problem description Dima loves representing an odd number as the sum of multiple primes, and Lisa lo ...

  10. B - Spyke Talks

    Problem description Polycarpus is the director of a large corporation. There are n secretaries worki ...