Advertisement
Guest User

Рабин для гениев

a guest
Aug 19th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <vector>
  2. #include <map>
  3. #include <set>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <cmath>
  8. #include <cstdio>
  9. #include <iomanip>
  10. #include <fstream>
  11. #include <cassert>
  12. #include <cstring>
  13. #include <unordered_set>
  14. #include <unordered_map>
  15. #include <numeric>
  16. #include <ctime>
  17. #include <bitset>
  18. #include <complex>
  19.  
  20. using namespace std;
  21.  
  22. #define cerr if (false) cerr
  23. #define int unsigned long long
  24.  
  25.  
  26. namespace Miller {
  27.     int p;
  28.     vector<int> need = {2, 3, 5, 7, 11, 17, 21, 27, 29};
  29.  
  30.     int binmul(int a, int b) {
  31.         int ans = 0;
  32.         while (b) {
  33.             if (b & 1) {
  34.                 ans += a;
  35.                 ans %= p;
  36.             }
  37.             b >>= 1;
  38.             if (b) {
  39.                 a += a;
  40.                 a %= p;
  41.             }
  42.         }
  43.         return ans;
  44.     }
  45.  
  46.  
  47.     int binpow(int a, int b) {
  48.         int ans = 1;
  49.         while (b) {
  50.             if (b & 1) {
  51.                 ans = binmul(ans, a);
  52.             }
  53.             b >>= 1;
  54.             if (b) {
  55.                 a = binmul(a, a);
  56.             }
  57.         }
  58.         return ans;
  59.     }
  60.  
  61.     int tr(int a) {
  62.         int q = p - 1;
  63.         while (q % 2 == 0) {
  64.             q /= 2;
  65.         }
  66.         int cp = q;
  67.         int cur = binpow(a, cp);
  68.         if (cur == 1 || cur == p - 1) {
  69.             return 1;
  70.         }
  71.         while (cp < p - 1) {
  72.             cp *= 2;
  73.             cur = binmul(cur, cur);
  74.             if (cur == p - 1) {
  75.                 return 1;
  76.             }
  77.         }
  78.         return 0;
  79.     }
  80.  
  81.     int solve(int x) {
  82.         if (x == 1) {
  83.             return 0;
  84.         }
  85.         p = x;
  86.         for (auto t : need) {
  87.             if (t == x) {
  88.                 return 1;
  89.             }
  90.             if (gcd(t, x) > 1) {
  91.                 return 0;
  92.             }
  93.             bool cr = tr(t);
  94.             //cout << t << ' ' << cr << endl;
  95.             if (!cr) {
  96.                 return 0;
  97.             }
  98.         }
  99.         //cout << "RETURN 1" << endl;
  100.         return 1;
  101.     }
  102. } //namespace Miller
  103.  
  104. signed main() {
  105.     ios_base::sync_with_stdio(false);
  106.     cin.tie(0);
  107.  
  108.     assert(freopen("again.in", "r", stdin));
  109.     assert(freopen("again.out", "w", stdout));
  110.  
  111.     int q;
  112.     cin >> q;
  113.     while (q--) {
  114.         int x;
  115.         cin >> x;
  116.         bool tmp = Miller::solve(x);
  117.         if (tmp) {
  118.             cout << "YES" << endl;
  119.         }  else {
  120.             cout << "NO" << endl;
  121.         }
  122.     }
  123.  
  124.     // Miller::p = 1e9 + 239;
  125.     // cout << Miller::binmul(5, 7) << endl;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement