-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMillerRobin.cpp
More file actions
78 lines (73 loc) · 1.6 KB
/
Copy pathMillerRobin.cpp
File metadata and controls
78 lines (73 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
if n is prime than, n−1=2s⋅d, with d odd.
Fermat's little theorem:
a^(n−1) ≡ 1 mod n
(a ^ (2^s) d) − 1 ≡ 0 mod n
(a ^ (2^(s−1) d) + 1 )(a ^ (2^(s−1) d) - 1 ) ≡ 0 mod n
(a ^ (2^(s−1) d) + 1 ) (a ^ (2^(s−2) d) + 1 )(a ^ (2^(s−2) d) - 1 )≡0modn
(a^2^(s−1) d + 1)(a^2^(s−2) d + 1)⋯(a^d+1)(a^d−1) ≡ 0 mod n
we check if either a^d ≡ 1 mod n
or a^((2^r)d) ≡ −1 mod n
holds for some 0 ≤ r ≤ s−1 .
*/
#include<bits/stdc++.h>
using namespace std;
using u64 = uint64_t;
using u128 = __uint128_t;
u64 bigmod(u64 base, u64 p, u64 mod)
{
u64 r=1;
base%=mod;
while(p)
{
if(p&1)
{
r=(u128) r*base % mod;
}
base = (u128) base*base %mod;
p>>=1;
}
return r;
}
bool isComposite(u64 n, u64 a, u64 d, int s)
{
u64 x = bigmod(a,d,n);
if(x==1 || x==n-1) return false;
for(int r=1; r<s ; r++)
{
x=(u128) x*x %n;
if(x==n-1) return false;
}
return true;
}
bool millerRobin(u64 n)
{
if(n<4) return (n==2||n==3);
int s=0;
u64 d=n-1;
while((d&1)==0)
{
d>>=1; s++;
}
int iter=10;
for(int i=0; i<iter; i++)
{
u64 a=2+rand()%(n-3);
if(isComposite(n,a,d,s)) return false;
}
return true;
}
int main()
{
int tc,cs=1,i,j;
cin>>tc;
while(tc--)
{
u64 n;
cin>>n;
bool is=millerRobin(n);
if(is) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}