-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiller-rabin.cpp
More file actions
66 lines (52 loc) · 961 Bytes
/
miller-rabin.cpp
File metadata and controls
66 lines (52 loc) · 961 Bytes
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
#include<iostream>
#include<stdc++.h>
using ll = long long;
using 128bit = __int128_t;
ll mod_mul(128bit a, 128bit b, 128bit mod){
return (128bit)a*b%mod;
}
ll pow_mod(ll a , ll b , ll mod){
ll ans = 1;
while(b >0){
if(a & 1){
res = mod_mul(a , b, mod);
}
b /= 2;
a = mod_mul(a ,a , mod);
}
return res;
}
bool miller_test(ll a , ll d , ll n){
ll x = pow_mod(a , d ,n);
if(x == 1 || x == n-1)return true;
while(d != n-1){
x = mod_mul(a ,a , n);
d <<=1;
if(x == 1)return false;
if(x == n-1)return true;
}
return false;
}
bool isPrime(ll n, int k = 10){
if(n < 2)return false;
if(n <= 3)return true;
if(n%2 == 0)return false;
ll d = n-1;
while((d&1)==0){
d>>=1;
}
for(int i{0};i<k;i++){
ll a = 2 + rand()%(n -4);
if(!miller_test(a ,d ,n)){
return false;
}
}
return true;
}
int main(){
int test;
cin>> test;
while(test--){
}
return 0;
}