To solve this problem, we need to efficiently handle two types of queries on an array: multiplying a range of elements by a given value and computing Euler's totient function for the product of elements in a specified range. The challenge is to do this efficiently given large constraints.
Approach
Prime Factorization Preprocessing:
Generate all primes up to 300 using the Sieve of Eratosthenes.
For each number from 1 to 300, precompute its prime factors. This helps quickly determine the primes involved in any multiplication query.
Inverse Modulo Precomputation:
Precompute the modular inverses of all primes modulo (10^9 + 7) using Fermat's Little Theorem. This is necessary to compute Euler's totient function efficiently.
Presence of Primes in Ranges:
For each prime (p), maintain a segment tree that tracks if (p) is a factor in any element of a given subrange. This allows us to quickly determine which primes divide the product of the range elements.
Product Modulo Segment Tree:
Use a segment tree to track the product of elements modulo (10^9 + 7). This allows efficient multiplication updates and product queries for any subrange.
Handling Queries:
Multiply Queries: Update the product modulo segment tree and propagate the presence of new primes across the range.
Totient Function Queries: Compute the product modulo (10^9 + 7) using the product segment tree. Then, calculate Euler's totient function using the precomputed primes' presence and their inverses.
Solution Code
cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int maxN = 4e5 + 100;
vector<int> primes;
vector<int> inv_prime;
vector<bool> is_prime(301, true);
unordered_map<int, vector<int>> factors;
class RangeMaxTree {
int n;
vector<int> tree;
vector<int> lazy;
void push(int node, int l, int r) {
if (lazy[node] == 1) {
tree[node] = 1;
if (l != r) {
lazy[2*node] = 1;
lazy[2*node+1] = 1;
}
lazy[node] = 0;
}
}
public:
RangeMaxTree(int size, vector<bool> &init) {
n = 1;
while (n < size) n *= 2;
tree.resize(2 * n);
lazy.resize(2 * n, 0);
// Initialize leaves
for (int i = 0; i < size; i++) {
tree[n + i] = init[i] ? 1 : 0;
}
for (int i = n - 1; i > 0; --i) {
tree[i] = max(tree[2*i], tree[2*i+1]);
}
}
void update_range(int a, int b) {
function<void(int, int, int, int, int)> update = [&](int node, int l, int r, int s, int e) {
push(node, l, r);
if (l > e || r < s) return;
if (s <= l && r <= e) {
lazy[node] = 1;
push(node, l, r);
return;
}
int mid = (l + r) / 2;
update(2*node, l, mid, s, e);
update(2*node+1, mid+1, r, s, e);
tree[node] = max(tree[2*node], tree[2*node+1]);
};
update(1, 0, n-1, a-1, b-1); // assuming 1-based to 0-based conversion
}
bool query_range(int a, int b) {
int res = 0;
function<void(int, int, int, int, int)> query = [&](int node, int l, int r, int s, int e) {
push(node, l, r);
if (l > e || r < s) return;
if (s <= l && r <= e) {
res = max(res, tree[node]);
return;
}
int mid = (l + r) / 2;
query(2*node, l, mid, s, e);
query(2*node+1, mid+1, r, s, e);
};
query(1, 0, n-1, a-1, b-1);
return res == 1;
}
};
class ProductTree {
int n;
vector<long long> tree;
vector<long long> lazy;
void push(int node, int l, int r) {
if (lazy[node] != 1) {
tree[node] = (tree[node] * lazy[node]) % MOD;
if (l != r) {
lazy[2*node] = (lazy[2*node] * lazy[node]) % MOD;
lazy[2*node+1] = (lazy[2*node+1] * lazy[node]) % MOD;
}
lazy[node] = 1;
}
}
public:
ProductTree(int size, vector<int>& a) {
n = 1;
while(n < size) n <<= 1;
tree.assign(2*n, 1);
lazy.assign(2*n, 1);
for (int i = 0; i < size; i++) {
tree[n + i] = a[i];
}
for (int i = n - 1; i > 0; --i) {
tree[i] = (tree[2*i] * tree[2*i+1]) % MOD;
}
}
void range_mul(int a, int b, long long val) {
function<void(int, int, int, int, int, long long)> update = [&](int node, int l, int r, int s, int e, long long v) {
push(node, l, r);
if (l > e || r < s) return;
if (s <= l && r <= e) {
lazy[node] = (lazy[node] * v) % MOD;
push(node, l, r);
return;
}
int mid = (l + r) >> 1;
update(2*node, l, mid, s, e, v);
update(2*node+1, mid+1, r, s, e, v);
tree[node] = (tree[2*node] * tree[2*node+1]) % MOD;
};
update(1, 0, n-1, a-1, b-1, val);
}
long long query(int a, int b) {
long long res = 1;
function<void(int, int, int, int, int)> query_range = [&](int node, int l, int r, int s, int e) {
push(node, l, r);
if (l > e || r < s) return;
if (s <= l && r <= e) {
res = (res * tree[node]) % MOD;
return;
}
int mid = (l + r) >> 1;
query_range(2*node, l, mid, s, e);
query_range(2*node+1, mid+1, r, s, e);
};
query_range(1, 0, n-1, a-1, b-1);
return res;
}
};
vector<RangeMaxTree> presence_trees;
void precompute() {
primes.clear();
is_prime[0] = is_prime[1] = false;
for (int p = 2; p <= 300; p++) {
if (is_prime[p]) {
primes.push_back(p);
for (int i = p*p; i <= 300; i += p) {
is_prime[i] = false;
}
}
}
inv_prime = vector<int>(301, 0);
for (int p : primes) {
inv_prime[p] = pow(p, MOD-2, MOD);
}
}
void initialize_primes(const vector<int>& a, int n, vector<RangeMaxTree>& presence_trees) {
// For each prime, initialize the tree
vector<bool> initial;
vector<int> N(a.size());
for (int i =0; i < primes.size(); i++) {
int p = primes[i];
initial.clear();
initial.reserve(n);
for (int val : a) {
initial.push_back(val % p == 0);
}
presence_trees.push_back(RangeMaxTree(n, initial));
}
}
int main() {
precompute();
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i =0; i < n; i++) cin >> a[i];
presence_trees.clear();
initialize_primes(a, n, presence_trees);
ProductTree product(n, a);
// Precompute factors for each x up to 300:
factors.clear();
factors.reserve(301);
for (int x = 0; x <=300; x++) {
unordered_set<int> f_set;
for (int p : primes) {
if (x % p == 0) f_set.insert(p);
}
factors[x] = vector<int>(f_set.begin(), f_set.end());
}
while (q--) {
string type;
cin >> type;
int l, r;
cin >> l >> r;
if (type[0] == 'M') {
// Multiply query
int x;
cin >> x;
auto& primes_factors = factors[x];
for (int p : primes_factors) {
int idx = find(primes.begin(), primes.end(), p) - primes.begin();
presence_trees[idx].update_range(l, r);
}
product.range_mul(l, r, x % MOD);
} else {
// TOTIENT query
long long prod_val = product.query(l, r);
long long term = 1;
for (size_t i = 0; i < primes.size(); i++) {
int p = primes[i];
if (presence_trees[i].query_range(l, r)) {
term = term * 1LL * (p-1) % MOD;
term = term * inv_prime[p] % MOD;
}
}
long long ans = (prod_val * term) % MOD;
cout << ans << endl;
}
}
return 0;
}
Explanation
Prime Generation: Use the Sieve of Eratosthenes to generate all primes up to 300.
Precompute Factors: For each number up to 300, store its unique prime factors.
Presence Management: Each prime has a segment tree that tracks if it exists in any element of a range. This allows O(log n) updates and queries.
Product Modulo Tracking: A segment tree tracks the product modulo (10^9 + 7), supporting range multiplicative updates and product queries.
Handling Queries:
Multiply Queries: Update the presence trees and product segment tree.
Tour Queries: Compute the product using the product tree and Euler's totient function using the precomputed primes' presence and inverses.
This approach efficiently handles the constraints with logarithmic time complexity for each query using appropriate segment trees and modular arithmetic.