To solve this problem, we need to efficiently handle two types of queries on an array of integers: multiplying a range of elements by a given value and computing Euler's totient function of the product of elements in a given range modulo (10^9 + 7).
Approach
-
Prime Factorization Preparation:
- Precompute primes up to 300: Since the maximum value in the array or the multiplier is 300, primes up to 300 are sufficient.
- Exponent Tables: For each integer from 1 to 300, precompute the exponents of all primes in its prime factorization.
-
Data Structures:
- Product Segment Tree: To handle range multiplicative updates and range product queries modulo (10^9 + 7).
- Exponent Segment Trees: Each prime has a segment tree that tracks the exponents of that prime in the current array elements. These allow range additions (for multiplying by values) and range sum queries.
-
Handling Queries:
- Multiply Query: Factorize the multiplier into primes, update the exponent for each prime in the corresponding segment trees, and update the product segment tree.
- TOTIENT Query:
- Compute the product of the range using the product segment tree modulo (10^9 + 7).
- For each prime, check if its cumulative exponent in the range is at least 1. If so, multiply the result by ((1-1/p)) modulo (10^9 + 7).
Solution Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
// Sieve of Eratosthenes to find all primes up to 300
void sieve(int n, vector<int>& primes) {
vector<bool> is_prime(n+1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i <= n; ++i) {
if (is_prime[i]) {
primes.push_back(i);
for (int j = i*i; j <= n; j += i) {
is_prime[j] = false;
}
}
}
}
// Precompute exponents for primes in each number up to 300
struct Exponents {
vector<int> arr; // primes are in fixed order from sieve
Exponents(int limit, const vector<int>& primes) : arr(primes.size(), 0) {
for (int p_idx = 0; p_idx < primes.size(); ++p_idx) {
int p = primes[p_idx];
int cnt = 0;
int num = limit;
while (num % p == 0) {
cnt++;
num /= p;
}
arr[p_idx] = cnt;
}
}
};
// Segment Tree for multiplicative range updates and product range queries
struct Product {
int n;
vector<ll> seg_tree;
vector<ll> lazy;
const vector<int>& primes;
vector<Exponents> exp_table; // precompute exponents for numbers 0..300
// ... but here for product, perhaps not needed
Product(int _size, const vector<int>& a, const vector<int>& primes_) : n(_size), primes(primes_) {
seg_tree.resize(4 * n);
lazy.resize(4 * n);
build(1, 1, n, a);
}
void build(int node, int l, int r, const vector<int>& a) {
if (l == r) {
seg_tree[node] = a[l % a.size()]; // assuming 1-based
lazy[node] = 1; // identity for multiplication
} else {
int mid = (l + r) / 2;
build(2*node, l, mid, a);
build(2*node+1, mid+1, r, a);
seg_tree[node] = (seg_tree[2*node] * seg_tree[2*node+1]) % MOD;
lazy[node] = 1;
}
}
// Push down lazy updates
void push(int node, int l, int r) {
if (lazy[node] != 1) {
seg_tree[node] = (seg_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;
}
}
void range_mult(int node, int l, int r, int a, int b, ll val) {
push(node, l, r);
if (a > r || b < l) return;
if (a <= l && r <= b) {
seg_tree[node] = (seg_tree[node] * val) % MOD;
if (l != r) {
lazy[2*node] = (lazy[2*node] * val) % MOD;
lazy[2*node+1] = (lazy[2*node+1] * val) % MOD;
}
return;
}
int mid = (l + r)/2;
range_mult(2*node, l, mid, a, b, val);
range_mult(2*node+1, mid+1, r, a, b, val);
seg_tree[node] = (seg_tree[2*node] * seg_tree[2*node+1]) % MOD;
}
// Query product from [a, b]
ll range_prod(int node, int l, int r, int a, int b) {
push(node, l, r);
if (a > r || b < l) return 1LL;
if (a <= l && r <= b) return seg_tree[node];
int mid = (l + r)/2;
return (range_prod(2*node, l, mid, a, b) * range_prod(2*node+1, mid+1, r, a, b)) % MOD;
}
void update(int a, int b, ll val) {
range_mult(1, 1, n, a, b, val);
}
ll query(int a, int b) {
return range_prod(1, 1, n, a, b);
}
};
// Segment Tree for range addition and sum queries
struct ExponentSegmentTree {
int n;
vector<int> seg_tree;
vector<int> lazy;
ExponentSegmentTree(int _size) : n(_size), seg_tree(4 * n), lazy(4 * n) {}
void build(int node, int l, int r, const vector<int>& a) {
if (l == r) {
seg_tree[node] = a[l];
lazy[node] = 0;
} else {
int mid = (l + r) / 2;
build(2*node, l, mid, a);
build(2*node+1, mid+1, r, a);
seg_tree[node] = seg_tree[2*node] + seg_tree[2*node+1];
lazy[node] = 0;
}
}
void push(int node, int l, int r) {
if (lazy[node] != 0) {
seg_tree[node] += lazy[node] * (r - l + 1);
if (l != r) {
lazy[2*node] += lazy[node];
lazy[2*node+1] += lazy[node];
}
lazy[node] = 0;
}
}
void range_add(int node, int l, int r, int a, int b, int val) {
push(node, l, r);
if (a > r || b < l) return;
if (a <= l && r <= b) {
seg_tree[node] += val * (r - l + 1);
if (l != r) {
lazy[2*node] += val;
lazy[2*node+1] += val;
}
return;
}
int mid = (l + r) / 2;
range_add(2*node, l, mid, a, b, val);
range_add(2*node+1, mid+1, r, a, b, val);
seg_tree[node] = seg_tree[2*node] + seg_tree[2*node+1];
}
int range_sum(int node, int l, int r, int a, int b) {
push(node, l, r);
if (a > r || b < l) return 0;
if (a <= l && r <= b) return seg_tree[node];
int mid = (l + r) / 2;
return range_sum(2*node, l, mid, a, b) + range_sum(2*node+1, mid+1, r, a, b);
}
void update(int a, int b, int val) {
range_add(1, 1, n, a, b, val);
}
int query(int a, int b) {
return range_sum(1, 1, n, a, b);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> a(n+1); // 1-based
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
// Precompute primes up to 300
vector<int> primes;
sieve(300, primes);
// Precompute inverse of all primes mod MOD
vector<ll> inv primes.size();
for (int i = 0; i < primes.size(); ++i) {
inv[i] = (ll) primes[i];
inv[i] = pow(inv[i], MOD-2, MOD);
inv[i] = (MOD + (primes[i]-1)*inv[i] % MOD) % MOD;
}
// Precompute exponents for every number up to 300
// Using a vector of Exponents structs for each number from 0 to 300
vector<vector<int>> exp_table(301, vector<int>(primes.size(), 0));
for (int x = 0; x <= 300; ++x) {
for (int p_idx = 0; p_idx < primes.size(); ++p_idx) {
int p = primes[p_idx];
int temp = x;
int cnt = 0;
while (temp % p == 0 && temp > 0) {
cnt++;
temp /= p;
}
exp_table[x][p_idx] = cnt;
}
}
// Initialize exponent for each prime
vector<vector<ExponentSegmentTree>> exponents(primes.size());
vector<vector<int>> exponents_initial(n+1, vector<int>(primes.size(),0));
for (int i = 1; i <= n; ++i) {
int val = a[i];
for (int p_idx = 0; p_idx < primes.size(); ++p_idx) {
exponents_initial[i][p_idx] = exp_table[val][p_idx];
}
}
// Build the exponent seg trees
vector<ExponentSegmentTree> seg(primes.size(), ExponentSegmentTree(n));
for (int p_idx = 0; p_idx < primes.size(); ++p_idx) {
vector<int> data(n+1, 0);
for (int i = 1; i <=n; ++i) {
data[i] = exponents_initial[i][p_idx];
}
seg[p_idx].build(1, 1, n, data);
}
// Product segment tree
Product prod_tree(n, a, primes);
while (q--) {
string type;
cin >> type;
if (type == "MULTIPLY") {
int l, r, x;
cin >> l >> r >> x;
// Decompose x into exponents
vector<int> exponents_x(primes.size(), 0);
for (int p_idx = 0; p_idx < primes.size(); ++p_idx) {
int p = primes[p_idx];
int cnt = 0;
int temp = x;
while (temp % p == 0 && temp > 0) {
cnt++;
temp /= p;
}
exponents_x[p_idx] = cnt;
}
// Update each prime's seg tree with exponents_x[p]
for (int p_idx = 0; p_idx < primes.size(); ++p_idx) {
if (exponents_x[p_idx] != 0) {
seg[p_idx].update(l, r, exponents_x[p_idx]);
}
}
// Update product tree
prod_tree.update(l, r, x);
} else {
int l, r;
cin >> l >> r;
ll prod = prod_tree.query(l, r);
ll phi_part = 1;
for (int p_idx = 0; p_idx < primes.size(); ++p_idx) {
int p = primes[p_idx];
int sum = seg[p_idx].query(l, r);
if (sum > 0) {
phi_part = (phi_part * inv[p_idx]) % MOD;
}
}
ll ans = (prod * phi_part) % MOD;
cout << ans << '\n';
}
}
return 0;
}
Explanation
- Sieve of Eratosthenes: We generate all primes up to 300.
- Exponent Tables: For each number from 0 to 300, precompute the exponent of each prime in its prime factorization.
- Segment Trees:
- Product Segment Tree: Supports range multiplicative updates and range product queries using lazy propagation.
- Exponent Segment Trees: One per prime, supporting range additions and sum queries to track cumulative exponents in ranges.
- Handling Queries:
- Multiply: Updates are decomposed into prime exponents, then applied to both the exponent trees and the product tree.
- TOTIENT: Computes the product using the product tree, then calculates (\phi) using the cumulative exponents from the exponent trees and precomputed inverses modulo (10^9 + 7).
This approach efficiently handles range operations and modular arithmetic to compute the required results within the problem constraints.