-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.cpp
83 lines (71 loc) · 1.49 KB
/
solution.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
#include <bits/stdc++.h>
using namespace std;
template <typename T = unsigned long long>
T modPow(T base, T exp, T mod) {
if (exp == 0) {
return 1;
}
if (exp == 1) {
return base % mod;
}
T res = modPow(base, exp / 2, mod);
res = res * res;
res %= mod;
if (exp & 1) {
res *= base;
res %= mod;
}
return res;
}
bool isPrime(unsigned long long n) {
using ULL = unsigned long long;
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0) return false;
int s = 0;
ULL d = n - 1;
while (d % 2 == 0) {
s++;
d >>= 1;
}
const auto _millerRabinTest = [n, s, d](ULL a) -> bool {
ULL x = modPow(a, d, n);
for (int i = 0; i < s; ++i) {
ULL y = (x * x) % n;
if (y == 1 && x != 1 && x != n - 1)
return false;
x = y;
}
return x == 1;
};
vector<ULL> testSet;
if (n < 2'047ull) testSet = {2};
else if (n < 1'373'653ull) testSet = {2, 3};
else if (n < 4'759'123'141ull) testSet = {2, 7, 61};
else if (n < 341'550'071'728'321ull) testSet = {2, 3, 5, 7, 11, 13, 17};
else testSet = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
for (const auto a : testSet) {
if (!_millerRabinTest(a)) {
return false;
}
}
return true;
}
void solve() {
int N;
scanf("%d", &N);
int count = 0;
while (N--) {
unsigned long long x;
scanf("%llu", &x);
count += isPrime(2 * x + 1);
}
printf("%d\n", count);
}
int main() {
int T = 1;
while (T--) {
solve();
}
return 0;
}