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 a range of elements modulo 10^9+7.
Approach
-
Prime Preprocessing:
- Sieve of Eratosthenes: Generate all primes up to 300 using the sieve method.
- Precompute Masks: For each integer from 1 to 300, compute a bitmask representing its prime factors.
- Precompute Terms: For each prime, compute the term (p-1) * modular multiplicative inverse of p modulo 10^9+7.
-
Data Structures:
- Prime Mask Segment Tree: This segment tree tracks the presence of primes in any subarray using bitwise OR operations and supports range updates.
- Product Segment Tree: This segment tree tracks the product of elements modulo 10^9+7 and supports range multiplication updates.
-
Handling Queries:
- Multiply Query: Update the relevant ranges in both segment trees. The Prime Mask Segment Tree updates to include new primes from the multiplier, while the Product Segment Tree updates the product.
- Totient Query: Compute the product using the Product Segment Tree. Determine the primes present in the range using the Prime Mask Segment Tree, then compute Euler's totient function using precomputed terms for these primes.
Solution Code
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
vector<int> primes;
vector<int> x_to_mask;
vector<int> terms;
// Precompute primes using sieve of Eratosthenes
void sieve() {
const int max_sieve = 300;
vector<bool> is_prime(max_sieve + 1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i <= max_sieve; ++i) {
if (is_prime[i]) {
primes.push_back(i);
for (int j = i * i; j <= max_sieve; j += i)
is_prime[j] = false;
}
}
}
// Precompute masks for numbers 1..300
void precompute_masks() {
x_to_mask.assign(301, 0);
for (int x = 1; x <= 300; ++x) {
int mask = 0;
if (x == 1) continue; // no primes
for (size_t i = 0; i < primes.size(); ++i) {
const int p = primes[i];
if (p > x) break;
if (x % p == 0) {
mask |= (1 << i);
}
}
x_to_mask[x] = mask;
}
}
// Precompute terms for each prime
void precompute_terms() {
terms.resize(primes.size());
for (size_t i = 0; i < primes.size(); ++i) {
const int p = primes[i];
ll inv_p = pow(p, MOD - 2, MOD);
terms[i] = ((p - 1) * inv_p) % MOD;
}
}
template<typename T>
struct SegmentTree {
int size;
vector<T> st, lazy;
T (*merge)(const T&, const T&);
T base;
SegmentTree(int n, T initial_base, function<T(T,T)> merge_func) {
merge = merge_func;
base = initial_base;
size = 1;
while (size < n) size <<= 1;
st.resize(2 * size, base);
lazy.resize(2 * size, 0);
}
void build(const vector<T>& v) {
for (int i = 0; i < v.size(); i++) st[size + i] = v[i];
for (int i = size - 1; i > 0; --i)
st[i] = merge(st[2*i], st[2*i+1]);
}
void push(int node, int l, int r) {
if (lazy[node] != 0 && node < size) {
st[2*node] = apply(st[2*node], lazy[node], node);
st[2*node+1] = apply(st[2*node+1], lazy[node], node);
lazy[2*node] = (lazy[2*node] + lazy[node]); // might vary based on function
lazy[2*node+1] = (lazy[2*node+1] + lazy[node]); // adjust per update function
lazy[node] = 0;
}
}
T apply(T val, T lazy_val, int node_size) {
// Implement specific application based on merge function
}
void range_update(int l, int r, T val) {
function<void(int, int, int)> update_helper =
[&](int node, int node_l, int node_r) {
if (node_r < l || node_l > r) return;
if (l <= node_l && node_r <= r) {
st[node] = apply(st[node], val, node_r - node_l + 1);
if (node < size) lazy[node] = val; // adjust this line based on your update
return;
}
push(node, node_l, node_r);
int mid = (node_l + node_r) / 2;
update_helper(2*node, node_l, mid);
update_helper(2*node+1, mid+1, node_r);
st[node] = merge(st[2*node], st[2*node+1]);
};
update_helper(1, 0, size - 1);
}
T query(int l, int r) {
T res = base;
function<void(int, int, int)> query_helper =
[&](int node, int node_l, int node_r) {
if (node_r < l || node_l > r) return;
if (l <= node_l && node_r <= r) {
res = merge(res, st[node]);
return;
}
push(node, node_l, node_r);
int mid = (node_l + node_r) / 2;
query_helper(2*node, node_l, mid);
query_helper(2*node+1, mid+1, node_r);
};
query_helper(1, 0, size - 1);
return res;
}
};
int main() {
sieve();
precompute_masks();
precompute_terms();
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int& x : a) cin >> x;
// PRIME MASK SEGMENT TREE
vector<int> initial_masks(n);
for (int i = 0; i < n;