当前位置:网站首页>Rabin-Miller素数测试
Rabin-Miller素数测试
2022-06-11 07:23:00 【CaptainHarryChen】
本文不证正确概率,因为我不会
判断大整数是否为质数
前提定理
- 费马小定理:若 n n 为质数,,则 an−1≡1 (mod n) a n − 1 ≡ 1 ( m o d n ) ;若任意整数x x 满足,则x x 有很大概率为质数;
- 定理2:若为大于2的质数,则a2≡1 (mod p) a 2 ≡ 1 ( m o d p ) 的解只能为a≡1 a ≡ 1 或a≡−1 a ≡ − 1 ;
Rabin-Miller素数测试
若待测数n n 为大于2的质数,则为偶数
设n−1=2s×d n − 1 = 2 s × d
随机一个小于n n 的整数
a2s×d≡1 (mod n) a 2 s × d ≡ 1 ( m o d n )
(a2s−1×d)2≡1 (mod n) ( a 2 s − 1 × d ) 2 ≡ 1 ( m o d n )
a2s−1×d≡1 (mod n) a 2 s − 1 × d ≡ 1 ( m o d n ) 或a2s−1×d≡−1 (mod n) a 2 s − 1 × d ≡ − 1 ( m o d n )
若为1,还可以继续递归,直到得到-1或者得到 ad≡±1 (mod n) a d ≡ ± 1 ( m o d n )
实现时,要倒着来
首先计算出ad a d ,判断其是否为±1 ± 1
然后不停的平方,得到a2×d,a22×d,a23×d...a2s×d a 2 × d , a 2 2 × d , a 2 3 × d . . . a 2 s × d
直到中途某一步得到了−1 − 1 ,则通过测试。
如果中途在得到-1之前,得到了1,则说明该数通过某一个不等于±1的数平方得到了1,不满足定理2,则该数测试不通过
一般都要随机取很多a a ,测试多次才能提高概率。
但是在OI中,用这一串a就行了
它保证3,825,123,056,546,413,051 3 , 825 , 123 , 056 , 546 , 413 , 051 以内的数计算正确(已经超过long long)
代码
版题:HDU2138
#include<cstdio>
const int TEST_VAL[]={
2,3,5,7,11,13,17,19,23,29,31,37,41},TEST_NUM=13;
long long PowMod(long long a,long long b,long long p)
{
long long res=1LL;
while(b)
{
if(b&1LL)
res=1LL*res*a%p;
a=1LL*a*a%p;
b>>=1LL;
}
return res;
}
bool MillerRabin(long long a,long long n,long long d)
{
long long x=PowMod(a,d,n);
if(x==1||x==n-1)
return true;
while(d<n-1)
{
x=1LL*x*x%n;
d=d*2LL;
if(x==1)
return false;
if(x==n-1)
return true;
}
return false;
}
bool isPrime(long long x)
{
if(x<10)
{
if(x==2||x==3||x==5||x==7)
return true;
return false;
}
long long d=x-1;
while((d&1LL)==0)
d>>=1LL;
for(int i=0;i<TEST_NUM;i++)
if(TEST_VAL[i]<x&&!MillerRabin(TEST_VAL[i],x,d))
return false;
return true;
}
int main()
{
int n,ans;
long long x;
while(scanf("%d",&n)!=EOF)
{
ans=0;
while(n--)
{
scanf("%I64d",&x);
ans+=isPrime(x);
}
printf("%d\n",ans);
}
return 0;
}边栏推荐
- Decimal to binary
- Calculate the day of the week for a specific month, year and day
- 20200730 T3 small B farm [maximum perimeter empty rectangle (monotone stack + line segment tree)] & "ROI 2017 day 2" learning track
- Outer margin collapse
- Configuration software -- control import
- [analysis of STL source code] summary notes (6): Design of iterator and magical traits
- Education expert wangzhongze shared his experience for many years: family education is not a vassal
- MS office level II wrong question record [6]
- Analyse du contrat du modèle de taux composé
- Android和iOS逆向分析/安全检测/渗透测试框架
猜你喜欢
随机推荐
The gap between the parent box and the child box
C language inherits memory management mechanism (unfinished)
Configuration software -- control drag and drop
May 30-June 5, 2022 AI industry weekly (issue 100): three years
MS office level II wrong question record [8]
R language Parallel Computing practice tutorial
【Oracle 数据库】奶妈式教程day03 排序查询
【Oracle 数据库】奶妈式教程day04 排序查询
2022 low voltage electrician test questions and online simulation test
Phi and phi (Mobius inversion + formula)
Ffmpeg extraction package format extracts AAC and customizes adtc header to realize arbitrary frame decoding
【CF#693 (Div. 3)】B. Fair Division
【CF#388 (Div. 2)】A. Bachgold Problem
【CF#697 (Div. 3)】 A - Odd Divisor
Education expert wangzhongze shared his experience for many years: family education is not a vassal
Gobang interface of mobile console (C language)
Compound ratemodel contract analysis
Nim product
P3327 [sdoi2015] approximate sum (Mobius inversion + formula)
C language to write a calculator calculation logic
![[analysis of STL source code] summary note (4): behind the scenes hero allocator](/img/b9/cf53fd8f933042ff65844d61eca55e.jpg)








