当前位置:网站首页>Rabin Miller prime test
Rabin Miller prime test
2022-06-11 07:36:00 【CaptainHarryChen】
This paper does not prove the correct probability , Because I won't
Determine whether large integers are prime numbers
Premise theorem
- Fermat's small Theorem : if n n Prime number ,, be an−1≡1 (mod n) a n − 1 ≡ 1 ( m o d n ) ; If any integer x x Satisfy , be x x There is a high probability that it is a prime number ;
- Theorem 2: if For more than 2 The prime number of , be a2≡1 (mod p) a 2 ≡ 1 ( m o d p ) The solution of can only be a≡1 a ≡ 1 or a≡−1 a ≡ − 1 ;
Rabin-Miller Prime number test
If the number to be measured n n For more than 2 The prime number of , be For the even
set up n−1=2s×d n − 1 = 2 s × d
Random one less than n n The integer of
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 ) or a2s−1×d≡−1 (mod n) a 2 s − 1 × d ≡ − 1 ( m o d n )
if 1, You can also continue recursion , Until get -1 Or get ad≡±1 (mod n) a d ≡ ± 1 ( m o d n )
The implementation , Turn it upside down
First, calculate ad a d , Judge whether it is ±1 ± 1
And then keep squaring , obtain a2×d,a22×d,a23×d...a2s×d a 2 × d , a 2 2 × d , a 2 3 × d . . . a 2 s × d
Until one step in the middle gets −1 − 1 , Pass the test .
If you get -1 Before , Got it 1, It means that the number is not equal to ±1 The square of the number of gets 1, Not satisfied with the theorem 2, Then the number test fails
Generally, you have to pick a lot at random a a , Test many times to improve the probability .
But in OI in , Use this string a That's it
It guarantees 3,825,123,056,546,413,051 3 , 825 , 123 , 056 , 546 , 413 , 051 The number within is calculated correctly ( Has more than long long)
Code
Edition title :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;
}边栏推荐
- 【AtCoder1984】Wide Swap (拓扑排序转化)
- I/o multiplexing - select/poll/epoll
- Classes and objects (Part 2)
- 2022年熔化焊接与热切割考试练习题及答案
- Bidirectional linked list simple template (pointer version)
- C language Yanghui triangle code
- RTMP protocol
- Simple configuration of vscade
- Find the longest common substring of N arrays (strings)
- 2022 melting welding and thermal cutting exam exercises and answers
猜你喜欢
![[analysis of STL source code] summary notes (3): vector introduction](/img/70/faa40c273f6b3a6b124fb870058489.jpg)
[analysis of STL source code] summary notes (3): vector introduction

Use of wordcloud

Several transaction modes of Seata

【软件测试】这样的简历已经刷掉了90%的面试者

QT table display data

JVM tuning

big. Js-- use / instance

Simple configuration of vscade

【IoT】智能硬件:如何获取硬件产品的wifi信号强度
![[Oracle database] mammy tutorial day04 Sorting Query](/img/79/9db26aa2d9dbb5514427edf03004f4.png)
[Oracle database] mammy tutorial day04 Sorting Query
随机推荐
C language function stack frame
MFC auxiliary CString string splicing
【AtCoder2306】Rearranging(拓扑)
MFC debugger OutputDebugString override
【集群】LVS+keepalived高可用集群
Use definite integral to calculate triangle area
Qstring to hexadecimal qstring
【IoT】智能硬件:如何获取硬件产品的wifi信号强度
2022.5.30-6.5 AI行业周刊(第100期):三年时光
【AtCoder1981】Shorten Diameter(图论思维)
Zero foundation self-study SQL course | outer join external connection
[Oracle database] mammy tutorial day02 use of database management tool sqlplus
2022低压电工操作证考试题模拟考试平台操作
【Oracle 数据库】奶妈式教程day02 数据库管理工具SQLPLUS的使用
Pointer to a two-dimensional array
Arduino_ STM development record
Bidirectional linked list simple template (pointer version)
Classes and objects (medium)
@Jsonproperty annotation
【AtCoder2307】Tree Game(博弈)