-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagain.cpp
65 lines (57 loc) · 1001 Bytes
/
pagain.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
#include <bits/stdc++.h>
using namespace std;
#define el '\n'
#define sp ' '
#define int unsigned long long
#define fu(i,a,b) for(int i=a;i<=b;++i)
#define CAZRALO_BO signed main(signed argc, char *argv[])
int power(int x, int y, int p) {
int res=1;
x%=p;
while (y) {
if (y&1) {
res=(res*x)%p;
}
y>>=1;
x=(x*x)%p;
}
return res;
}
bool rabinMiller(int n,int d) {
int a=2+rand()%(n-4);
int x=power(a,d,n);
if (x==1 || x==n-1) return true;
while (d!=n-1) {
x=(x*x)%n;
d<<=1;
if (x==1) return false;
if (x==n-1) return true;
}
return false;
}
bool isPrime(int n,int k) {
if (n<2 || n==4) return false;
if (n<=3) return true;
int d=n-1;
while (d%2==0) { d>>=1; }
fu(i,1,k) {
if (!rabinMiller(n,d)) return false;
}
return true;
}
CAZRALO_BO {
cin.tie(0)->sync_with_stdio(0); cout.tie(0);
int testcase;
cin>>testcase;
while (testcase--) {
int n;
cin>>n;
n--;
while (1) {
if (isPrime(n,2)) break;
--n;
}
cout<<n<<el;
}
return 0;
}