a53

Expresii

a53
Jan 21st, 2022 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int M = 1000000;
  6. const int MOD = 1e9 + 7;
  7.  
  8. array<int, M + 1> small;
  9.  
  10. struct Repr {
  11. list<pair<int, int>> a, b;
  12. bool a_inf = false;
  13. };
  14.  
  15. void intersect(const list<pair<int, int>> &a, const list<pair<int, int>> &b,
  16. list<pair<int, int>> &ret) {
  17. static array<int, M + 1> auxa, auxb;
  18. for (auto t : a)
  19. auxa[t.first] = max(auxa[t.first], t.second);
  20. for (auto t : b)
  21. auxb[t.first] = max(auxb[t.first], t.second);
  22. for (auto t : a) {
  23. int e = min(auxa[t.first], auxb[t.first]);
  24. if (e)
  25. ret.emplace_back(t.first, e);
  26. auxa[t.first] = 0;
  27. }
  28. for (auto t : b)
  29. auxb[t.first] = 0;
  30. }
  31.  
  32. void Parse(istream &cin, Repr &ret) {
  33. string tok;
  34. cin >> tok;
  35. if (tok == "[") {
  36. Parse(cin, ret);
  37. Parse(cin, ret);
  38. } else if (tok == "(") {
  39. Repr l, r;
  40. Parse(cin, l);
  41. Parse(cin, r);
  42. intersect(l.b, r.b, ret.b);
  43. if (l.a_inf && r.a_inf) {
  44. ret.a.clear();
  45. ret.a_inf = true;
  46. } else if (l.a_inf) {
  47. ret.a.splice(ret.a.end(), r.a);
  48. } else if (r.a_inf) {
  49. ret.a.splice(ret.a.end(), l.a);
  50. } else {
  51. intersect(l.a, r.a, ret.a);
  52. }
  53. } else if (tok == "x") {
  54. ret.a.clear();
  55. ret.a_inf = true;
  56. } else {
  57. int val = stoi(tok);
  58. while (val > 1) {
  59. int p = small[val], e = 0;
  60. while (val % p == 0)
  61. val /= p, e += 1;
  62. ret.a.emplace_back(p, e);
  63. ret.b.emplace_back(p, e);
  64. }
  65. }
  66. }
  67.  
  68. int main() {
  69. ifstream cin("expresii.in");
  70. ofstream cout("expresii.out");
  71.  
  72. for (int i = 2; i <= M; ++i) {
  73. if (!small[i]) {
  74. for (int j = i; j <= M; j += i)
  75. small[j] = i;
  76. }
  77. }
  78.  
  79. string s;
  80. cin >> s;
  81. string ns;
  82. for (auto &c : s) {
  83. if (!isdigit(c))
  84. ns += ' ';
  85. if (c != ']' && c != ')' && c != ',')
  86. ns += c;
  87. if (!isdigit(c))
  88. ns += ' ';
  89. }
  90. // cerr << ns << endl;
  91. stringstream ss(ns);
  92. Repr repr;
  93. Parse(ss, repr);
  94. static array<int, M + 1> a_map, b_map;
  95. for (auto t : repr.b)
  96. b_map[t.first] = max(b_map[t.first], t.second);
  97. for (auto t : repr.a)
  98. a_map[t.first] = max(a_map[t.first], t.second);
  99.  
  100. int base = 1;
  101. for (int i = 1; i <= M; ++i)
  102. for (int j = 0; j < b_map[i]; ++j)
  103. base = 1LL * base * i % MOD;
  104.  
  105. int a, b;
  106. cin >> a >> b;
  107. int total = 0;
  108. for (int i = a; i <= b; ++i) {
  109. int now = base, val = i;
  110. while (val > 1) {
  111. int p = small[val], e = 0;
  112. while (val % p == 0)
  113. val /= p, e += 1;
  114. if (!repr.a_inf)
  115. e = min(e, a_map[p]);
  116. for (int j = b_map[p]; j < e; ++j)
  117. now = 1LL * now * p % MOD;
  118. }
  119. // stringstream ss(ns);
  120. // assert(now == Brut(ss, i) % MOD);
  121. //cerr << "i = " << i << " ans = " << now << endl;
  122. total = (total + now) % MOD;
  123. }
  124. cout << total << endl;
  125. return 0;
  126. }
Add Comment
Please, Sign In to add comment