Advertisement
a53

PrimeQuery_Of

a53
May 13th, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class AIB {
  4. public :
  5. AIB() {}
  6. AIB(int _n) : n(_n) {
  7. aib = vector<int>(n + 1);
  8. }
  9. inline void Update(int pos) {
  10. for (int i = pos; i <= n; i += i & -i)
  11. ++aib[i];
  12. }
  13. inline int Query(int pos) {
  14. int sum(0);
  15. for (int i = pos; i > 0; i -= i & -i)
  16. sum += aib[i];
  17. return sum;
  18. }
  19. private :
  20. int n;
  21. vector<int> aib;
  22. };
  23. typedef unsigned long long i64;
  24. inline i64 Multiply(i64 a, i64 b, i64 MOD) {
  25. i64 res(0);
  26. while (b) {
  27. if (b & 1) {
  28. res += a;
  29. if (res >= MOD)
  30. res -= MOD;
  31. }
  32. a += a;
  33. if (a >= MOD)
  34. a -= MOD;
  35. b >>= 1;
  36. }
  37. return res;
  38. }
  39. inline i64 Powlog(i64 a, i64 b, i64 MOD) {
  40. i64 res(1);
  41. while (b) {
  42. if (b & 1)
  43. res = Multiply(res, a, MOD);
  44. a = Multiply(a, a, MOD);
  45. b >>= 1;
  46. }
  47. return res;
  48. }
  49. inline bool Prim(i64 n) {
  50. if (n < 2) return false;
  51. if (n == 2 || n == 3 || n == 5) return true;
  52. if (n % 2 == 0 || n % 3 == 0 || n % 5 == 0) return false;
  53. i64 d(n - 1);
  54. while (d % 2 == 0)
  55. d >>= 1;
  56. for (int i = 1; i <= 2; ++i) {
  57. bool ch(true);
  58. i64 a = 2 + rand() % (n - 4), temp(d);
  59. i64 x = Powlog(a, temp, n);
  60. if (x == 1 || x == n - 1)
  61. continue;
  62. while (temp != n - 1) {
  63. x = Multiply(x, x, n);
  64. temp <<= 1;
  65. if (x == 1)
  66. return false;
  67. if (x == n - 1) {
  68. ch = false;
  69. break;
  70. }
  71. }
  72. if (ch) return false;
  73. }
  74. return true;
  75. }
  76. int n, ind, x;
  77. vector<i64> v, numbers;
  78. unordered_map<i64, int> pos;
  79. int main() {
  80. ios_base::sync_with_stdio(false);
  81. ifstream fin("primequery.in");
  82. fin >> n;
  83. v = numbers = vector<i64>(n);
  84. for (int i = 0; i < n; ++i) {
  85. fin >> v[i];
  86. numbers[i] = v[i];
  87. }
  88. fin.close();
  89. sort(numbers.begin(), numbers.end());
  90. numbers.erase(unique(numbers.begin(), numbers.end()), numbers.end());
  91. for (const i64& x : numbers)
  92. pos[x] = ++ind;
  93. AIB arb(ind);
  94. ofstream fout("primequery.out");
  95. for (int i = 0; i < n; ++i) {
  96. x = pos[v[i]];
  97. fout << arb.Query(x - 1) << ' ';
  98. if (Prim(v[i]))
  99. arb.Update(x);
  100. }
  101. fout.close();
  102. return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement