fork download
  1. #include <bits/stdc++.h>
  2. #define BUFF ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
  3. #define FOR(a,b,i) for(int i=(a);i<=(b);i++)
  4. #define REV(a,b,i) for(int i=(a);i>=(b);i--)
  5. #define LOW(v,x) lower_bound(v.begin(),v.end(),x)
  6. #define UP(v,x) upper_bound(v.begin(),v.end(),x)
  7. #define SZ(x) ((int)x.size())
  8. #define pb push_back
  9. #define fi first
  10. #define se second
  11. #define INP(a) freopen(a,"r",stdin);
  12. #define OUT(a) freopen(a,"w",stdout);
  13.  
  14. template<typename T>bool maximize(T& x,const T& y){return (x<y)?x=y,1:0;}
  15. template<typename T>bool minimize(T& x,const T& y){return (x>y)?x=y,1:0;}
  16.  
  17. using namespace std;
  18. using ll=long long;
  19. using pa=pair<int,int>;
  20. using str=string;
  21.  
  22. const int INF=1e9;
  23. const int N=1e5+1;
  24. const int A=1e3+1;
  25.  
  26. ll randInt(ll l,ll r){
  27. return l+rand()%(r-l+1);
  28. }
  29.  
  30. ll multiply(ll a,ll k,ll n){
  31. ll res=0;
  32. while(k){
  33. if(k&1)res=(res+a)%n;
  34. a=(a+a)%n;
  35. k/=2;
  36. }
  37. return res;
  38. }
  39.  
  40. ll binaryPower(ll a,ll k,ll n){
  41. // if(k==0)return 1;
  42. ll res=1;
  43. while(k){
  44. if(k&1)res=multiply(res,a,n);
  45. a=multiply(a,a,n);
  46. k/=2;
  47. }
  48. return res;
  49. }
  50.  
  51. bool checkPrime(ll n){
  52. if(n==2||n==3)return 1;
  53. if(n<3||n%2==0||n%3==0)return 0;
  54.  
  55. FOR(1,100,i){
  56. ll x=randInt(2,n-1);
  57. if(binaryPower(x,n-1,n)!=1)return 0;
  58. }
  59. return 1;
  60. }
  61.  
  62. void solve(){
  63. int t;
  64. cin>>t;
  65. while(t--){
  66. ll n;
  67. cin>>n;
  68. cout<<(checkPrime(n)?"YES\n":"NO\n");
  69. }
  70. }
  71.  
  72. int main(){
  73. srand(time(nullptr));
  74. // INP("fermat.inp")
  75. BUFF
  76. solve();
  77. return 0;
  78. }
  79.  
Success #stdin #stdout 0.02s 5324KB
stdin
10
1000000007
1000005277
1000002277
41041
561
2
90
10
113
41
stdout
YES
YES
YES
NO
NO
YES
NO
NO
YES
YES