To solve this problem efficiently, we need to handle two types of queries on an array of integers: multiplying a range by a given value and computing Euler's totient function of the product of a range. The key challenge is to efficiently track the prime factors of the elements and compute the totient function without explicitly calculating products, which can be infeasibly large.
Approach
-
Prime Factorization Precomputation:
- Precompute all primes up to 300 using the Sieve of Eratosthenes.
- Precompute the prime factors for all integers from 1 to 300 to quickly determine the prime factors of any multiplier
x in the queries.
-
Segment Trees for Exponent Tracking:
- For each prime, maintain a segment tree to track the exponents of that prime in the elements of the array. This allows efficient range updates (when multiplying by a factor) and range sum queries (to determine the total exponent over a range).
-
Handling Queries:
- Multiply Query: Factorize the multiplier into primes and update the corresponding segment trees to add the exponents across the queried range.
- Totient Query: For each prime, retrieve the total exponent in the queried range. Use these exponents to compute Euler's totient function using the formula
φ(N) = N * product(1 - 1/p) for the primes dividing N. This is done by calculating the product as defined by the prime exponents and their contributions.
Solution Code
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int MOD = 1e9 + 7;
vector<int> primes;
vector<vector<int>> prime_factors;
// Modular exponentiation helper function
LL mod_pow(LL a, LL b, LL mod) {
LL res = 1;
a %= mod;
while (b) {
if (b % 2 == 1) res = res * a % mod;
a = a * a % mod;
b /= 2;
}
return res;
}
class SegTree {
private:
int size;
vector<int> tree;
vector<int> lazy;
public:
SegTree(const vector<int>& arr) {
size = arr.size();
tree.resize(4 * size);
lazy.resize(4 * size);
build(0, 0, size - 1, arr);
}
void build(int node, int l, int r, const vector<int>& arr) {
if (l == r) {
tree[node] = arr[l];
return;
}
int mid = (l + r) / 2;
build(2 * node + 1, l, mid, arr);
build(2 * node + 2, mid + 1, r, arr);
tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
}
void range_add(int l, int r, int val) {
rec_add(0, 0, size - 1, l, r, val);
}
int range_sum(int l, int r) {
return rec_query(0, 0, size - 1, l, r);
}
void rec_push(int node, int l, int r, int mid) {
if (lazy[node] != 0) {
tree[2 * node + 1] += lazy[node] * (mid - l + 1);
lazy[2 * node + 1] += lazy[node];
tree[2 * node + 2] += lazy[node] * (r - (mid + 1) + 1);
lazy[2 * node + 2] += lazy[node];
lazy[node] = 0;
}
}
void rec_add(int node, int l, int r, int L, int R, int val) {
if (L > r || R < l) return;
if (L <= l && R >= r) {
tree[node] += val * (r - l + 1);
lazy[node] += val;
return;
}
int mid = (l + r) / 2;
rec_push(node, l, r, mid);
rec_add(2 * node + 1, l, mid, L, R, val);
rec_add(2 * node + 2, mid + 1, r, L, R, val);
tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
}
int rec_query(int node, int l, int r, int L, int R) {
if (L > r || R < l) return 0;
if (L <= l && R >= r) return tree[node];
int mid = (l + r) / 2;
rec_push(node, l, r, mid);
return rec_query(2 * node + 1, l, mid, L, R) +
rec_query(2 * node + 2, mid + 1, r, L, R);
}
};
vector<int> generate_primes(int n) {
vector<bool> is_prime(n + 1, true);
is_prime[0] = is_prime[1] = false;
for (int p = 2; p * p <= n; ++p) {
if (is_prime[p]) {
for (int i = p * p; i <= n; i += p) {
is_prime[i] = false;
}
}
}
vector<int> res;
for (int i = 2; i <= n; ++i) {
if (is_prime[i]) res.push_back(i);
}
return res;
}
void precompute_factors() {
int max_x = 300;
primes = generate_primes(300);
int num_primes = primes.size();
prime_factors.resize(max_x + 1);
for (int x = 1; x <= max_x; ++x) {
prime_factors[x].resize(num_primes, 0);
int curr = x;
for (int j = 0; j < num_primes; ++j) {
int p = primes[j];
if (p > curr) break;
if (curr % p != 0) continue;
int ex = 0;
while (curr % p == 0) {
ex++;
curr /= p;
}
prime_factors[x][j] = ex;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
precompute_factors();
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int& x : a) cin >> x;
int num_primes = primes.size();
vector<vector<int>> prime_exponents(num_primes, vector<int>(n));
for (int i = 0; i < n; ++i) {
int x = a[i];
for (int j = 0; j < num_primes; ++j) {
prime_exponents[j][i] = prime_factors[x][j];
}
}
vector<SegTree> seg_trees;
for (int j = 0; j < num_primes; ++j) {
seg_trees.emplace_back(prime_exponents[j]);
}
while (q--) {
string type;
cin >> type;
if (type == "MULTIPLY") {
int l, r, x;
cin >> l >> r >> x;
l--, r--;
vector<int> exponents = prime_factors[x];
for (int j = 0; j < num_primes; ++j) {
int val = exponents[j];
if (val) {
seg_trees[j].range_add(l, r, val);
}
}
} else {
int l, r;
cin >> l >> r;
l--, r--;
LL result = 1;
for (int j = 0; j < num_primes; ++j) {
int ex = seg_trees[j].range_sum(l, r);
if (ex == 0) continue;
int p = primes[j];
LL term = ((LL(p - 1) % MOD) * mod_pow(p, ex - 1, MOD)) % MOD;
result = (result * term) % MOD;
}
cout << result << '\n';
}
}
return 0;
}
Explanation
-
Prime Facorization Setup: Primes up to 300 are precomputed. For every number from 1 to 300, we store the exponents of each prime in its factorization.
-
Segment Trees: Each prime has a segment tree to track the exponents in the array through range updates and queries.
-
Handling Queries:
- Multiply Queries: When multiplying a range by
x, decompose x into its prime factors and update the corresponding segment trees with the exponents.
- Totient Queries: For each prime, sum the exponents in the queried range. The totient function is computed using the formula derived from Euler's product formula, avoiding direct computation of large products.
This approach ensures efficient handling of large ranges and updates, leveraging the properties of segment trees and prime factorization.