-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMillarRobin_primality_test.cpp
More file actions
58 lines (53 loc) · 1.29 KB
/
MillarRobin_primality_test.cpp
File metadata and controls
58 lines (53 loc) · 1.29 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
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll modpower(ll a, ll b, ll mod){
ll x = 0,y = a % mod;
while (b > 0) {
if (b % 2 == 1)
x = (x + y) % mod;
y = (y * 2) % mod;
b /= 2;
}
return x % mod;
}
ll bigmod(ll base, ll exponent, ll mod){
ll x = 1;
ll y = base%mod;
while (exponent > 0){
if (exponent % 2 == 1)
x = modpower(x,y,mod);
y = modpower(y,y,mod);
exponent = exponent / 2;
}
return x % mod;
}
bool millarRobinIsPrime(ll p,int iteration){
if (p < 2) return false;
if (p != 2 && p % 2==0) return false;
ll s = p - 1;
while (s % 2 == 0) s /= 2;
for (int i = 0; i <= iteration; i++) {
time_t t;
srand((unsigned) time(&t));
ll a = rand() % (p - 1) + 1, temp = s;
ll mod = bigmod(a, temp, p);
while (temp != p - 1 && mod != 1 && mod != p - 1) {
mod = modpower(mod, mod, p);
temp *= 2;
}
if (mod != p - 1 && temp % 2 == 0) return false;
}
return true;
}
int main() {
int test;
scanf("%d",&test);
while(test--){
ll num;
scanf("%lld",&num);
if(millarRobinIsPrime(num,50)) printf("YES\n");
else printf("NO\n");
}
return 0;
}