a53

PrimeQuery_TESTER

a53
May 12th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <algorithm>
  3. #define Nmax 15001
  4. #define ull unsigned long long
  5.  
  6. using namespace std;
  7.  
  8. ifstream fin("primequery.in");
  9. ofstream fout("primequery.out");
  10.  
  11. unsigned long long V[Nmax] , C[Nmax] ;
  12. int D[Nmax];
  13.  
  14. ull power(ull x, ull y, ull p)
  15. {
  16. ull res = 1;
  17. x = x % p;
  18.  
  19. while (y > 0)
  20. {
  21. if (y & 1)
  22. res = (res*x) % p;
  23.  
  24. y = y / 2;
  25. x = (x * x) % p;
  26. }
  27. return res;
  28. }
  29.  
  30. bool miillerTest(ull d, ull n)
  31. {
  32. ull a = 2 + rand() % (n - 4);
  33.  
  34. ull x = power(a, d, n);
  35.  
  36. if (x == 1 || x == n-1)
  37. return true;
  38.  
  39. while (d != n-1)
  40. {
  41. x = (x * x) % n;
  42. d *= 2;
  43.  
  44. if (x == 1) return false;
  45. if (x == n-1) return true;
  46. }
  47.  
  48. return false;
  49. }
  50.  
  51. bool isPrime(ull n, ull k)
  52. {
  53. if (n <= 1 || n == 4) return false;
  54. if (n <= 3) return true;
  55. if(n == 5) return true;
  56.  
  57. ull d = n - 1;
  58. while (d % 2 == 0)
  59. d /= 2;
  60.  
  61. for (ull i = 0; i < k; i++)
  62. if (!miillerTest(d, n))
  63. return false;
  64.  
  65. return true;
  66. }
  67. int main()
  68. {
  69. int n , k = 0;
  70.  
  71. fin >> n;
  72.  
  73. for(int i = 1 ; i <= n ; i++)
  74. {
  75. fin >> V[i];
  76.  
  77. if(isPrime(V[i] , 600))
  78. C[++k] = V[i];
  79.  
  80. D[i] = k;
  81. }
  82. for(int i = 1 ; i <= n ; i++)
  83. {
  84. int c = 0;
  85.  
  86. for(int j = 1 ; j <= D[i] ; j++)
  87. {
  88. if(C[j] < V[i])
  89. c++;
  90. }
  91.  
  92. fout << c << " ";
  93. }
  94. }
Add Comment
Please, Sign In to add comment