-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPON.c
65 lines (62 loc) · 857 Bytes
/
PON.c
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>
#define ull unsigned long long int
using namespace std;
ull modmul(ull a, ull b, ull c)
{
ull res=0;
a%=c;
b%=c;
while(b)
{
if(b%2==1) res=(res+a)%c;
a=(a*2)%c;
b/=2;
}
return res%c;
}
ull power(ull a, ull p, ull mod)
{
ull res=1;
a%=mod;
while(p)
{
if(p%2==1) res=modmul(res,a,mod);
a=modmul(a,a,mod);
p/=2;
}
return res;
}
bool fermat(ull p)
{
srand(time(NULL));
ull c;
int i=2;
if(p%2==0 || p%3==0) return false;
while(i--)
{
c=rand()%(p-1)+1;
if(power(c,p-1,p)%p!=1)
{
return false;
}
}
return true;
}
int main()
{
int t;
cin>>t;
while(t--)
{
ull n;
cin>>n;
if(n==2 || n==3)
{
cout<<"YES"<<endl;
continue;
}
if(fermat(n)) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}