Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int M = 1000000;
- const int MOD = 1e9 + 7;
- array<int, M + 1> small;
- struct Repr {
- list<pair<int, int>> a, b;
- bool a_inf = false;
- };
- void intersect(const list<pair<int, int>> &a, const list<pair<int, int>> &b,
- list<pair<int, int>> &ret) {
- static array<int, M + 1> auxa, auxb;
- for (auto t : a)
- auxa[t.first] = max(auxa[t.first], t.second);
- for (auto t : b)
- auxb[t.first] = max(auxb[t.first], t.second);
- for (auto t : a) {
- int e = min(auxa[t.first], auxb[t.first]);
- if (e)
- ret.emplace_back(t.first, e);
- auxa[t.first] = 0;
- }
- for (auto t : b)
- auxb[t.first] = 0;
- }
- void Parse(istream &cin, Repr &ret) {
- string tok;
- cin >> tok;
- if (tok == "[") {
- Parse(cin, ret);
- Parse(cin, ret);
- } else if (tok == "(") {
- Repr l, r;
- Parse(cin, l);
- Parse(cin, r);
- intersect(l.b, r.b, ret.b);
- if (l.a_inf && r.a_inf) {
- ret.a.clear();
- ret.a_inf = true;
- } else if (l.a_inf) {
- ret.a.splice(ret.a.end(), r.a);
- } else if (r.a_inf) {
- ret.a.splice(ret.a.end(), l.a);
- } else {
- intersect(l.a, r.a, ret.a);
- }
- } else if (tok == "x") {
- ret.a.clear();
- ret.a_inf = true;
- } else {
- int val = stoi(tok);
- while (val > 1) {
- int p = small[val], e = 0;
- while (val % p == 0)
- val /= p, e += 1;
- ret.a.emplace_back(p, e);
- ret.b.emplace_back(p, e);
- }
- }
- }
- int main() {
- ifstream cin("expresii.in");
- ofstream cout("expresii.out");
- for (int i = 2; i <= M; ++i) {
- if (!small[i]) {
- for (int j = i; j <= M; j += i)
- small[j] = i;
- }
- }
- string s;
- cin >> s;
- string ns;
- for (auto &c : s) {
- if (!isdigit(c))
- ns += ' ';
- if (c != ']' && c != ')' && c != ',')
- ns += c;
- if (!isdigit(c))
- ns += ' ';
- }
- // cerr << ns << endl;
- stringstream ss(ns);
- Repr repr;
- Parse(ss, repr);
- static array<int, M + 1> a_map, b_map;
- for (auto t : repr.b)
- b_map[t.first] = max(b_map[t.first], t.second);
- for (auto t : repr.a)
- a_map[t.first] = max(a_map[t.first], t.second);
- int base = 1;
- for (int i = 1; i <= M; ++i)
- for (int j = 0; j < b_map[i]; ++j)
- base = 1LL * base * i % MOD;
- int a, b;
- cin >> a >> b;
- int total = 0;
- for (int i = a; i <= b; ++i) {
- int now = base, val = i;
- while (val > 1) {
- int p = small[val], e = 0;
- while (val % p == 0)
- val /= p, e += 1;
- if (!repr.a_inf)
- e = min(e, a_map[p]);
- for (int j = b_map[p]; j < e; ++j)
- now = 1LL * now * p % MOD;
- }
- // stringstream ss(ns);
- // assert(now == Brut(ss, i) % MOD);
- //cerr << "i = " << i << " ans = " << now << endl;
- total = (total + now) % MOD;
- }
- cout << total << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment