[模板] Miller-Rabin 素数测试
细节挺多的。。
#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 素数测试的更多相关文章
- POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...
- HDU1164_Eddy'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 ...
- Miller Rabin素数检测与Pollard Rho算法
一些前置知识可以看一下我的联赛前数学知识 如何判断一个数是否为质数 方法一:试除法 扫描\(2\sim \sqrt{n}\)之间的所有整数,依次检查它们能否整除\(n\),若都不能整除,则\(n\)是 ...
- POJ2429_GCD & LCM Inverse【Miller Rabin素数測试】【Pollar Rho整数分解】
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 ...
- POJ1811_Prime Test【Miller Rabin素数測试】【Pollar Rho整数分解】
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...
- Miller Rabin素数检测
#include<iostream> #include<cstdio> #include<queue> #include<cstring> #inclu ...
- Miller Rabbin素数测试
步骤 ①先写快速幂取模函数 ②MR算法开始 (1)传入两个参数一个是底数一个是n也就是幂数,如果n是一个合数那么可以判定,这个数一定不是素数 (2)然后开始寻找一个奇数的n去计算,如果最后满足a^d% ...
- 关于素数:求不超过n的素数,素数的判定(Miller Rabin 测试)
关于素数的基本介绍请参考百度百科here和维基百科here的介绍 首先介绍几条关于素数的基本定理: 定理1:如果n不是素数,则n至少有一个( 1, sqrt(n) ]范围内的的因子 定理2:如果n不是 ...
- 与数论的厮守01:素数的测试——Miller Rabin
看一个数是否为质数,我们通常会用那个O(√N)的算法来做,那个算法叫试除法.然而当这个数非常大的时候,这个高增长率的时间复杂度就不够这个数跑了. 为了解决这个问题,我们先来看看费马小定理:若n为素数, ...
- 【数论基础】素数判定和Miller Rabin算法
判断正整数p是否是素数 方法一 朴素的判定
随机推荐
- POJ3468【线段树lazy操作】
上午理论AC,打到现在快吐了... 一个那么**Lazy操作打成这样,query操作和update操作都有问题,妈蛋,发现是mid<=s+1-真是蠢到家,明明是mid+1<=s卧槽连左和右 ...
- New Land LightOJ - 1424
New Land LightOJ - 1424 题意:找出01矩阵中最大的完全由0组成的矩阵. 方法: 重点在于转化. 先预处理(i,j)点向上最长能取到的连续的全0条的长度.然后枚举某一行作为矩阵的 ...
- h5-19-文件操作-文件域
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Excel 宏练习
任务描述: 利用 Excel 绘制函数图像 f(x)=x^2/3+0.9*(3.3-x^2)^1/2*sin(a*x),并通过按钮事件来刷新图像. 问题分析: 可以参考类似 Matlab 绘图的方式, ...
- C. Tennis Championship dp递推 || 找规律
http://codeforces.com/contest/735/problem/C C. Tennis Championship time limit per test 2 seconds mem ...
- SAMBA服务初学练习
服务概述 Samba最先在Linux和Windows之间架起了一座桥梁,正是由于Samba的出现,我们可以在Linux和Windows之间实现文件共享的相互通讯,我们可以将其架设成一个功能非常强大的文 ...
- 基于webmagic的爬虫小应用
以前没有写过爬虫程序,最近两天就研究了一下java的爬虫框架webmagic.然后写了一个demo 写爬虫的基本思想: 1.抓取目标连接 2.根据页面中标签,抓捕你需要的内容 3.保存结果集 以下是实 ...
- nodejs中相互引用(循环引用)的模块分析
话不多少,直接上源码吧: modA.js: module.exports.test = 'A'; const modB = require('./05_modB'); console.log( 'mo ...
- Android开发实现高德地图定位
1.获取Key 参考官方文档:http://lbs.amap.com/api/android-location-sdk/guide/create-project/get-key 对于签名文件的获取建议 ...
- Android原生系统API自带dp、px、sp单位转换
Android系统中自带的Api中可以使用TypedValue进行单位转换 1,调用系统api转换单位 // 获得转换后的px值 float pxDimension = TypedValue.appl ...