-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMiller-Rabin Primaility test.cpp
181 lines (143 loc) · 2.98 KB
/
Miller-Rabin Primaility test.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define y1 lalalalalala
#define index lalalalalalala
#define show(a, i) cout << "a[" << i << "] = " << a[i] << endl;
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define ll unsigned long long
const int INF = 1e9 + 1;
int getint()
{
char ch;
int neg = 1;
int val = 0;
while(ch = getchar())
{
if(ch == '-') neg = -1;
else if(ch > 57 || ch < 48) break;
else val = 10 * val + ch - 48;
}
val = val * neg;
return val;
}
const int cs = 1e5 + 1;
ll n;
vector<ll>p;
void getprimes()
{
int up = (int)sqrt(1.e9 + 1);
vector<int>u(up + 1, 0);
for(int i = 2 ; i <= (int)sqrt(up) ; i ++)
if(!u[i])
for(int j = i * i ; j < up ; j += i)
u[j] = 1;
for(int i = 2 ; i < up ; i ++)
if(!u[i])
p.pb(i);
}
ll mul(ll a, ll b, ll c)
{
if(b == 0)
return 0;
ll temp = mul(a, b >> 1, c);
temp = temp << 1;
temp %= c;
if(b & 1)
{
temp += a;
temp %= c;
}
return temp;
}
ll modpow(ll a, ll b, ll c)
{
if(b == 0)
return 1;
ll temp = modpow(a, b >> 1, c);
temp = mul(temp, temp, c);
if(b & 1)
temp = mul(temp, a, c);
return temp;
}
bool pseudoprime(ll n)
{
return modpow(2, n - 1, n) == 1;
}
int Tries(ll n)
{
/// returns how many numbers will be checked as a witness
/// if it is big, error rate will so little...
return (int)log2(n) + 1;
}
bool Witness(ll r, ll n)
{
ll m = n - 1;
int s = 0;
while(!(m & 1))
{
s ++;
m = m >> 1;
}
vector<ll>x(s + 1, 0);
x[0] = modpow(r, m, n);
for(int i = 1 ; i <= s ; i ++)
{
x[i] = mul(x[i - 1], x[i - 1], n);
if(x[i] == 1 && x[i - 1] != 1 && x[i - 1] != n - 1)
return true;
}
if(x[s] != 1)
return true;
return false;
}
bool solve_small(ll n)
{
if(n == 1)
return false;
int up = (int)sqrt(n);
for(auto i : p)
{
if(i > up)
break;
if(n % i == 0)
return false;
}
return true;
}
bool Miller_Rabin(ll n)
{
if(n < (1 << 30))
return solve_small(n);
if(!pseudoprime(n))
return false;
int s = Tries(n);
for(int i = 0 ; i < s ; i ++)
{
ll r = p[i];
if(r >= n)
break;
if(n % r == 0 || Witness(r, n) == true)
return false;
}
return true;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(false);
getprimes();
int t;
cin >> t;
while(t --)
{
cin >> n;
if(Miller_Rabin(n) == true)
cout << "YES" << endl;
else cout << "NO" << endl;
}
///cerr << "Elapsed time: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec" << endl;
return 0;
}