细节挺多的。。

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<ctime> using namespace std; typedef long long ll; ll mul(ll a,ll b,ll mod) {
ll ret = 0ll;
a %= mod;
while( b ) {
if ( b & 1ll ) ret = ( ret + a ) % mod, b--;
b >>= 1ll;
a = ( a + a ) % mod;
}
return ret;
} ll qpow(ll a,ll b,ll mod) {
ll ret = 1ll;
a %= mod;
while( b ) {
if ( b & 1ll ) ret = mul(ret,a,mod),b--;
b >>= 1ll;
a = mul(a,a,mod);
}
return ret;
} ll ter[]= {,,,,,,,,,,};
const int TOP=;
bool Miller_Rabin(ll n) {
if ( n==2ll||n==3ll ) return true;
if ( !( n & 1ll ) ) return false;
ll d = n - 1ll;
int s = ;
while( !( d & 1ll ) ) ++s, d>>=1ll;
for(int i=; i<=TOP; i++) {
ll a = ter[i];
if(a>=n) return true;
ll x = qpow(a,d,n);
ll y = 0ll;
for(int j=; j<s; j++) {
y = mul(x,x,n);
if ( 1ll == y && 1ll != x && n-1ll != x ) return false;
x = y;
}
if ( 1ll != y ) return false;
}
return true;
} int main() {
ll x;
while(cin>>x) {
Miller_Rabin(x)?cout<<"YES\n":cout<<"NO\n";
}
return ;
}

[模板] 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. Miller Rabin素数检测与Pollard Rho算法

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

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

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

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

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

  6. Miller Rabin素数检测

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

  7. Miller Rabbin素数测试

    步骤 ①先写快速幂取模函数 ②MR算法开始 (1)传入两个参数一个是底数一个是n也就是幂数,如果n是一个合数那么可以判定,这个数一定不是素数 (2)然后开始寻找一个奇数的n去计算,如果最后满足a^d% ...

  8. 关于素数:求不超过n的素数,素数的判定(Miller Rabin 测试)

    关于素数的基本介绍请参考百度百科here和维基百科here的介绍 首先介绍几条关于素数的基本定理: 定理1:如果n不是素数,则n至少有一个( 1, sqrt(n) ]范围内的的因子 定理2:如果n不是 ...

  9. 与数论的厮守01:素数的测试——Miller Rabin

    看一个数是否为质数,我们通常会用那个O(√N)的算法来做,那个算法叫试除法.然而当这个数非常大的时候,这个高增长率的时间复杂度就不够这个数跑了. 为了解决这个问题,我们先来看看费马小定理:若n为素数, ...

  10. 【数论基础】素数判定和Miller Rabin算法

    判断正整数p是否是素数 方法一 朴素的判定   

随机推荐

  1. VMware安装Centos 7,网络连接问题以及解决方案

    转自: https://www.cnblogs.com/owaowa/p/6123902.html 在这里表示万分感谢 在使用CentOS虚拟机后,出现了无法上网的情况,使用主机ping虚机地址可以p ...

  2. 模拟赛01 T3 盖房子

    题面 http://zhengruioi.com/problem/248 题解 三重容斥(说是两重也行吧) 我们来看题目的约束 ①有k个位置不能放(k≤8) ②每行每列至少一个 ③正负对角线至少一个 ...

  3. 洛谷 P2147 [SDOI2008]洞穴勘测

    以下这个做法应该是叫线段树分治... 根据修改操作预处理出每条边存在的时间区间[l,r](以操作序号为时间),然后把所有形式化后的修改挂到线段树节点上. 处理完修改后,dfs一遍线段树,进入某个节点时 ...

  4. 转 sql 查出一张表中重复的所有记录数据

    select * from DB_PATCH awhere lower(a.db_name) in (select lower(db_name) from DB_PATCH group by lowe ...

  5. webkit滤镜

    -webkit-filter: grayscale(1);/*灰度*/ -webkit-filter: sepia(1);/*褐色*/ -webkit-filter: saturate(1);/*饱和 ...

  6. MVC 下 ajax调用 日期差值计算

    背景: 服务项目已有服务期起止时间From-To 现在要根据用户输入的新的起始时间, 和该服务期的原有区间值, 计算出新的服务期截止时间 即 NewServiceToDateTime = NewSer ...

  7. Tomcat+Jenkins+SonarQube+SVN+Maven 集成自动化环境搭建(Windows10环境下)

    说在前面的话: 从接到任务到完成共用了7天的时间.正常人用不到这个时间. 此时的功能表现是: 登录本地JenKins对项目进行构建,能够自动从SVN读取最新代码并按照Maven项目构建,构建完成能够自 ...

  8. CF778A(round 402 div.2 D) String Game

    题意: Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. B ...

  9. CF750C New Year and Rating

    题意: 在cf系统中,给定一个人每次比赛所属的div及比赛之后的分数变化.计算经过这些比赛之后此人的rating最高可能是多少. 思路: 模拟. 实现: #include <cstdio> ...

  10. [BZOJ1040][ZJOI2008]骑士 基环树DP

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1040 题目给出了$n$个点和$n$条无向边,即一棵基环树或者基环树森林. 如果题目给的关系 ...