Coding Challenge Alpha VIII ( By AlgoRave DTU ) Editorial

Revision en14, by MadhavCoding, 2025-03-30 15:30:17

We'd like to thank you all for participating in the contest, and hope you enjoyed it. Hope to see you again soon!

A – Try and Cry

Author: BitWizz

Tutorial
Implementation

A – Make All Equal

Author: neal27

Hint 1
Hint 2
Tutorial
Implementation

B – Your Own CPU Scheduler

Author: neal27

Hint 1
Hint 2
Tutorial
Implementation

D – Equalizing an Array

Author: Utkarsh59

Hint 1
Hint 2
Tutorial
Implementation

A – AP Wars

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;
}

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en41 English vanshdhawan60 2025-04-01 21:22:52 0 (published)
en40 English BitWizz 2025-04-01 21:18:06 132 (saved to drafts)
en39 English _bLIC_ 2025-04-01 20:27:44 1 final commit (published)
en38 English _bLIC_ 2025-04-01 20:27:01 3
en37 English _bLIC_ 2025-04-01 20:26:29 1460 removed sample solution
en36 English Lazy_Seg_Tree 2025-03-31 23:41:14 170
en35 English Lazy_Seg_Tree 2025-03-31 23:35:23 4 Tiny change: 'in range [$l$, $r$].\n\n</sp' -> 'in range [l, r].\n\n</sp'
en34 English Lazy_Seg_Tree 2025-03-31 23:34:40 14
en33 English Lazy_Seg_Tree 2025-03-31 23:34:19 4887 Tiny change: 'oiler>\n\n' -> 'oiler>\n\n\n\n<h3> Aditya and Gambling Addiction </h3>\n\n'
en32 English ShivamKapoor23 2025-03-30 22:16:00 122 Reverted to en29
en31 English ShivamKapoor23 2025-03-30 22:15:16 14
en30 English ShivamKapoor23 2025-03-30 22:13:53 112
en29 English ShivamKapoor23 2025-03-30 22:12:17 131
en28 English ShivamKapoor23 2025-03-30 22:10:20 89
en27 English ShivamKapoor23 2025-03-30 22:09:07 30
en26 English ShivamKapoor23 2025-03-30 22:08:16 112
en25 English ShivamKapoor23 2025-03-30 22:06:03 79
en24 English ShivamKapoor23 2025-03-30 21:57:47 55
en23 English ShivamKapoor23 2025-03-30 21:55:54 119
en22 English ShivamKapoor23 2025-03-30 21:53:34 32
en21 English ShivamKapoor23 2025-03-30 21:51:26 22 Tiny change: 'iler>\n\n\n[<h3>C &' -> 'iler>\n\n\usepackage{amsmath} \n[<h3>C &'
en20 English ShivamKapoor23 2025-03-30 21:46:50 4470
en19 English MadhavCoding 2025-03-30 16:17:57 6 Tiny change: 'ing,2025-03-30] <br>\n\n' -> 'ing,2025-02-11] <br>\n\n'
en18 English MadhavCoding 2025-03-30 16:09:31 2 Tiny change: 'n\n\n[<h3>A &ndash; A' -> 'n\n\n[<h3>E &ndash; A'
en17 English MadhavCoding 2025-03-30 16:03:28 12
en16 English MadhavCoding 2025-03-30 15:51:43 10 Tiny change: 'for each \textbf{Result ' -> 'for each \bf{Result '
en15 English MadhavCoding 2025-03-30 15:45:33 275
en14 English MadhavCoding 2025-03-30 15:30:17 6300
en13 English Utkarsh59 2025-03-30 13:43:59 3196 Tiny change: '_1$):Take a[k]$ as t' -> '_1$):Take $a[k]$ as t'
en12 English neal27 2025-03-30 11:48:25 1 Tiny change: '_x/b_x$ < p_y/b_y$. ' -> '_x/b_x$ < $p_y/b_y$. '
en11 English neal27 2025-03-30 11:40:37 2 Tiny change: '>\n\n[<h3>A &ndash; Y' -> '>\n\n[<h3>B &ndash; Y'
en10 English neal27 2025-03-30 11:35:31 58
en9 English neal27 2025-03-30 11:33:06 75
en8 English neal27 2025-03-30 11:31:53 18
en7 English neal27 2025-03-30 11:30:09 685
en6 English neal27 2025-03-30 11:15:04 2939
en5 English neal27 2025-03-30 10:57:02 3 Tiny change: '(n - 1)/2}}$.\n</spo' -> '(n - 1)/2}$.\n</spo'
en4 English neal27 2025-03-30 10:55:38 608
en3 English neal27 2025-03-30 10:53:37 1246
en2 English _bLIC_ 2025-03-30 03:30:24 1
en1 English _bLIC_ 2025-03-30 03:28:56 1633 initial commit (saved to drafts)