forked from algorithmzuo/algorithm-journey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode02_LargeNumberIsPrime3.java
More file actions
76 lines (62 loc) · 1.62 KB
/
Code02_LargeNumberIsPrime3.java
File metadata and controls
76 lines (62 loc) · 1.62 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
// 判断较大的数字是否是质数(Miller-Rabin测试)
// C++同学可以提交如下代码
// 可以通过所有测试用例,核心在于第11行,整型成了128位
// 测试链接 : https://www.luogu.com.cn/problem/U148828
/*
#include <bits/stdc++.h>
using namespace std;
typedef __int128 ll;
typedef pair<int, int> pii;
template<typename T> inline T read() {
T x = 0, f = 1; char ch = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch - '0');
return x * f;
}
template<typename T> inline void write(T x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) write(x / 10);
putchar(x % 10 + '0');
}
template<typename T> inline void print(T x, char ed = '\n') {
write(x), putchar(ed);
}
ll t, n;
ll qpow(ll a, ll b, ll mod) {
ll ret = 1;
while(b) {
if(b & 1) ret = (ret * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ret % mod;
}
vector<ll> p = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
bool miller_rabin(ll n) {
if(n < 3 || n % 2 == 0) return n == 2;
ll u = n - 1, t = 0;
while(u % 2 == 0) u /= 2, ++ t;
for(auto a : p) {
if(n == a) return 1;
if(n % a == 0) return 0;
ll v = qpow(a, u, n);
if(v == 1) continue;
ll s = 1;
for(; s <= t; ++ s) {
if(v == n - 1) break;
v = v * v % n;
}
if(s > t) return 0;
}
return 1;
}
int main() {
t = read<ll>();
while(t --) {
n = read<ll>();
if(miller_rabin(n)) puts("Yes");
else puts("No");
}
return 0;
}
*/