forked from shunr/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbf3hard.c
More file actions
58 lines (54 loc) · 1012 Bytes
/
bf3hard.c
File metadata and controls
58 lines (54 loc) · 1012 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
#include <stdbool.h>
#include <stdio.h>
#include <math.h>
#define ll long long
ll modpow(ll a, ll b, ll n) {
__int128 x = 1;
__int128 y = a;
while (b > 0){
if (b % 2 == 1) {
x = x * y % n;
}
y = y * y % n;
b = b / 2;
}
return x % n;
}
bool isprime(ll n) {
if (n == 2) {
return true;
}
else if (n <= 1 || n % 2 == 0) {
return false;
}
ll rt = pow(n,0.5);
for (ll i = 3; i <= rt; i+=2) {
if (n % i == 0) {
return false;
}
}
return true;
}
bool fermat(ll n) {
if (n == 1) {
return false;
}
for (int i = 0; i < 2; i++) {
ll rng = (rand() * rand()) % (n - 1) + 2;
if (modpow(rng, n - 1, n) != 1) {
return false;
}
}
return true;
}
int main() {
ll meme;
scanf("%lld", &meme);
while (meme < 1000000000 && isprime(meme) == false) {
meme += 1;
}
while (meme > 1000000000 && fermat(meme) == false) {
meme += 1;
}
printf("%lld", meme);
}