We'd like to thank you all for participating in the contest, and hope you enjoyed it. Hope to see you again soon!
Author: BitWizz
Author: neal27
Author: neal27
Author: Utkarsh59
Author: MadhavCoding
The answer will never be -1.
First try to solve for two Arithmetic Progressions.
Suppose we have two Arithmetic Progressions with the same starting value $$$a$$$ and common differences $$$d_1$$$ and $$$d_2$$$.
$$$AP_1 :$$$ $$$a, a+d_1, a+2d_1,....$$$
$$$AP_2 :$$$ $$$a, a+d_2, a+2d_2,....$$$
The first common element is $$$a$$$. Let the next common element be $$$a + k$$$.
Hence, the common difference of the common elements of both the Arithmetic Progressions will be $$$k$$$. We now calculate $$$k$$$.
Assume that the next common element appears at the $$$(n_1 - 1) ^ {th}$$$ term in $$$AP_1$$$ and at the $$$(n_2 - 1) ^ {th}$$$ term in $$$AP_2$$$ (with the first term corresponding to $$$n = 0$$$).
Then we have:
$$$\implies a + k = a + n_1d_1 = a + n_2d_2$$$
$$$\implies k = n_1d_1 = n_2d_2$$$
$$$\implies d_1 \mid k$$$ and $$$d_2 \mid k$$$
Thus, both $$$d_1$$$ and $$$d_2$$$ divides $$$k$$$, which means that $$$k$$$ must be a common multiple of $$$d_1$$$ and $$$d_2$$$. Since we are interested in the smallest such positive $$$k$$$, it follows that:
$$$\implies k = \operatorname{lcm}(d_1, d_2)$$$
Therefore, the common difference of the common elements of both the Arithmetic Progressions is $$$\operatorname{lcm}(d_1, d_2)$$$.
Extending this observation to multiple arithmetic progressions, for each \textbf{Result Query} $$$[l, r]$$$, we need to return:
$$$\operatorname{lcm}(d_l, d_{l+1}, d_{l+2},....,d_r)$$$.
To efficiently handle both Update and Range queries, we can utilize a data structure like Segment Tree.
/*
Author : MadhavCoding
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define pi pair<int, int>
#define pll pair<ll, ll>
#define endl "\n"
const ll MOD = 1e9 + 7;
const ll N = 1e3;
using namespace std;
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<ll, ll, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_map;
vector <bool> isPrime(N+1, true);
vector <long long> prime;
void sieve()
{
for (ll i = 2; i*i <= N; i++)
{
if (isPrime[i])
{
for (int j = i*i; j <= N; j=j+i)
{
isPrime[j] = false;
}
}
}
for(ll i = 2; i <= N; i++)
{
if(isPrime[i]) prime.push_back(i);
}
}
ll modmul(ll a, ll b)
{
return ((a % MOD) * (b % MOD)) % MOD;
}
ll modpower(ll a, ll b, ll m)
{
if(b==0) return 1;
else if (b%2==0)
{
int t = modpower(a, b/2, m);
return (1ll * t * t%m);
}
else
{
int t = modpower(a, b/2, m);
t = 1ll*t*t%m;
return (1ll * a * t%m);
}
}
class SegTree
{
ll size;
vector<vector<ll>> sums;
ll n1 = prime.size();
vector<ll> NEUTRAL;
public:
void init(ll n)
{
size = 1;
while(size < n) size *= 2;
NEUTRAL.resize(n1, 0);
sums.resize(2 * size - 1, NEUTRAL);
}
void build(vector<ll>& a, ll x, ll lx, ll rx)
{
if(rx - lx == 1)
{
if(lx < (ll)a.size())
{
ll temp = a[lx];
// cout<<temp<<endl;
for(int i = 0; i < n1; i++)
{
sums[x][i] = 0;
while(temp % prime[i] == 0)
{
sums[x][i]++;
temp /= prime[i];
}
}
// if(temp != 1) cout<<"temp : "<<temp<<endl;
// for(auto i : sums[x]) cout<<i<<" ";
// cout<<endl;
}
return;
}
ll m = lx + (rx - lx) / 2;
build(a, 2 * x + 1, lx, m);
build(a, 2 * x + 2, m, rx);
for(int i = 0; i < n1; i++)
{
sums[x][i] = max(sums[2 * x + 1][i], sums[2 * x + 2][i]);
}
}
void build(vector<ll>& a)
{
build(a, 0, 0, size);
}
void set_x(ll i, ll v, ll x, ll lx, ll rx)
{
if(rx - lx == 1)
{
ll temp = v;
for(int i = 0; i < n1; i++)
{
sums[x][i] = 0;
while(temp % prime[i] == 0)
{
sums[x][i]++;
temp /= prime[i];
}
}
return;
}
ll m = lx + (rx - lx) / 2;
if(i < m) set_x(i, v, 2 * x + 1, lx, m);
else set_x(i, v, 2 * x + 2, m, rx);
for(int i = 0; i < n1; i++)
{
sums[x][i] = max(sums[2 * x + 1][i], sums[2 * x + 2][i]);
}
}
void set_x(ll i, ll v)
{
set_x(i, v, 0, 0, size);
}
ll find_sum(ll l, ll r, ll x, ll lx, ll rx, ll ind)
{
if(r <= lx || rx <= l) return 0;
else if(l <= lx && r >= rx) return sums[x][ind];
ll m = lx + (rx - lx) / 2;
ll s1 = find_sum(l, r, 2 * x + 1, lx, m, ind);
ll s2 = find_sum(l, r, 2 * x + 2, m, rx, ind);
return max(s1, s2);
}
ll find_sum(ll l, ll r)
{
ll res = 1;
for(int i = 0; i < n1; i++)
{
ll pr = find_sum(l, r, 0, 0, size, i);
res = modmul(res, modpower(prime[i], pr, MOD));
}
return res;
}
};
void solve()
{
ll n, q;
cin>>n>>q;
vector<ll> vec(n);
for (int i = 0; i < n; i++)
{
cin>>vec[i];
}
SegTree st;
st.init(n);
st.build(vec);
while (q--)
{
int op; cin>>op;
if(op == 1)
{
ll ind, num;
cin>>ind>>num;
ind--;
st.set_x(ind, num);
}
else
{
ll l, r;
cin>>l>>r;
l--; r--;
cout<<st.find_sum(l, r + 1)<<endl;
}
}
}
int main(int argc, char const *argv[])
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr); std::cout.tie(nullptr);
sieve();
int t = 1;
cin>>t;
while(t--)
{
solve();
}
return 0;
}




