Boring Queries (Codeforces 1422F)
Tags: Math, number theory

Yura owns a quite ordinary and boring array $$$a$$$ of length $$$n$$$. You think there is nothing more boring than that, but Vladik doesn't agree!In order to make Yura's array even more boring, Vladik makes $$$q$$$ boring queries. Each query consists of two integers $$$x$$$ and $$$y$$$. Before answering a query, the bounds $$$l$$$ and $$$r$$$ for this query are calculated: $$$l = (last + x) \bmod n + 1$$$, $$$r = (last + y) \bmod n + 1$$$, where $$$last$$$ is the answer on the previous query (zero initially), and $$$\bmod$$$ is the remainder operation. Whenever $$$l > r$$$, they are swapped.After Vladik computes $$$l$$$ and $$$r$$$ for a query, he is to compute the least common multiple (LCM) on the segment $$$[l; r]$$$ of the initial array $$$a$$$ modulo $$$10^9 + 7$$$. LCM of a multiset of integers is the smallest positive integer that is divisible by all the elements of the multiset. The obtained LCM is the answer for this query.Help Vladik and compute the answer for each query!

Input: The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the array.The second line contains $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$) — the elements of the array.The third line contains a single integer $$$q$$$ ($$$1 \le q \le 10^5$$$) — the number of queries.The next $$$q$$$ lines contain two integers $$$x$$$ and $$$y$$$ each ($$$1 \le x, y \le n$$$) — the description of the corresponding query.

Output: Print $$$q$$$ integers — the answers for the queries.

Note: Consider the example:  boundaries for first query are $$$(0 + 1) \bmod 3 + 1 = 2$$$ and $$$(0 + 3) \bmod 3 + 1 = 1$$$. LCM for segment $$$[1, 2]$$$ is equal to $$$6$$$;  boundaries for second query are $$$(6 + 3) \bmod 3 + 1 = 1$$$ and $$$(6 + 3) \bmod 3 + 1 = 1$$$. LCM for segment $$$[1, 1]$$$ is equal to $$$2$$$;  boundaries for third query are $$$(2 + 2) \bmod 3 + 1 = 2$$$ and $$$(2 + 3) \bmod 3 + 1 = 3$$$. LCM for segment $$$[2, 3]$$$ is equal to $$$15$$$;  boundaries for fourth query are $$$(15 + 2) \bmod 3 + 1 = 3$$$ and $$$(15 + 3) \bmod 3 + 1 = 1$$$. LCM for segment $$$[1, 3]$$$ is equal to $$$30$$$. 

Sample Inputs:
3
2 3 5
4
1 3
3 3
2 3
2 3

Sample Outputs:
6
2
15
30

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def fast_prime_factorization(n):
    d = [0 if i & 1 else 2 for i in range(n + 1)]
    (d[0], d[1]) = (0, 1)
    for i in range(3, n + 1):
        if d[i]:
            continue
        for j in range(i, n + 1, 2 * i):
            if not d[j]:
                d[j] = i
    return d

def f(u, v):
    return u * l2 + v

def g(u, v):
    return u * l + v

def get_segment(s, t):
    (s, t) = (s + l1, t + l1)
    ans = []
    while s <= t:
        if s & 1:
            ans.append(s)
            s += 1
        s >>= 1
        if not t & 1:
            ans.append(t)
            t -= 1
        t >>= 1
    return ans
n = int(input())
mod = pow(10, 9) + 7
l = 2 * pow(10, 5) + 5
a = [0] + list(map(int, input().split()))
d = fast_prime_factorization(l)
(x, z, c) = ([0] * l, [], 0)
h = 58
for i in range(2, h):
    if d[i] == i:
        x[i] = c
        z.append(i)
        c += 1
b = [0] * (n + 1)
l1 = pow(2, (n + 1).bit_length())
l2 = 2 * l1
tree1 = [1] * (16 * l2)
v = [[] for _ in range(n + 1)]
la = [0] * l
for i in range(1, n + 1):
    u = a[i]
    v0 = []
    while u ^ d[u]:
        if d[u] < h:
            tree1[f(x[d[u]], i + l1)] *= d[u]
        else:
            v0.append(d[u])
        u //= d[u]
    if 2 <= u <= h:
        tree1[f(x[u], i + l1)] *= d[u]
    elif u <= 450:
        v0.append(u)
    elif 450 < u:
        b[i] = u
    if v0:
        if len(v0) == 1 or v0[0] ^ v0[1]:
            for j in v0:
                if la[j]:
                    v[la[j]].append(g(i, j))
                else:
                    v[0].append(g(i, j))
                la[j] = i
        else:
            k = v0[0]
            v0[1] *= k
            for j in v0:
                if la[j]:
                    v[la[j]].append(g(i, k))
                else:
                    v[0].append(g(i, k))
                la[j] = i
for i in range(0, 16 * l2, l2):
    for j in range(l1 - 1, 0, -1):
        tree1[i + j] = max(tree1[i + 2 * j], tree1[i + 2 * j + 1])
tree2 = [1] * l2
nx = [0] * (n + 1)
la = [0] * l
for i in range(1, n + 1):
    u = b[i]
    if not u:
        continue
    if la[u]:
        nx[la[u]] = i
    else:
        tree2[i + l1] = u
    la[u] = i
for i in v[0]:
    (j, k) = divmod(i, l)
    tree2[j + l1] = k * tree2[j + l1] % mod
for i in range(l1 - 1, 0, -1):
    tree2[i] = tree2[2 * i] * tree2[2 * i + 1] % mod
now = [1] * l2
for i in range(1, n + 1):
    if nx[i]:
        u = nx[i] + l1
        while u:
            now[u] += 1
            u >>= 1
    for j in v[i]:
        u = j // l + l1
        while u:
            now[u] += 1
            u >>= 1
now[0] = 0
for i in range(1, l2):
    now[i] += now[i - 1]
now = [0] + now
tree3 = [1] * now[l2]
s0 = [0] * now[l2]
for i in range(1, l2):
    tree3[now[i]] = tree2[i]
for i in range(1, n + 1):
    if nx[i]:
        u = nx[i] + l1
        s0[now[u]] = i
        now[u] += 1
        tree2[u] = b[i] * tree2[u] % mod
        tree3[now[u]] = tree2[u]
        u >>= 1
        while u:
            s0[now[u]] = i
            now[u] += 1
            tree2[u] = tree2[2 * u] * tree2[2 * u + 1] % mod
            tree3[now[u]] = tree2[u]
            u >>= 1
    for j in v[i]:
        (u, k) = divmod(j, l)
        u += l1
        s0[now[u]] = i
        now[u] += 1
        tree2[u] = k * tree2[u] % mod
        tree3[now[u]] = tree2[u]
        u >>= 1
        while u:
            s0[now[u]] = i
            now[u] += 1
            tree2[u] = tree2[2 * u] * tree2[2 * u + 1] % mod
            tree3[now[u]] = tree2[u]
            u >>= 1
for i in range(1, l2):
    s0[now[i]] = n
    now[i] += 1
now = [0] + now
q = int(input())
ans = []
last = 0
pow2 = [1]
for _ in range(20):
    pow2.append(2 * pow2[-1])
for _ in range(q):
    (x, y) = map(int, input().split())
    (l, r) = ((last + x) % n + 1, (last + y) % n + 1)
    if l > r:
        (l, r) = (r, l)
    ans0 = 1
    s = get_segment(l, r)
    for i in range(16):
        m = 0
        for j in s:
            if m < tree1[f(i, j)]:
                m = tree1[f(i, j)]
        ans0 = ans0 * m % mod
    for i in s:
        (j, k) = (now[i], now[i + 1])
        c = (k - j).bit_length()
        for u in range(c, -1, -1):
            if j + pow2[u] < k and s0[j + pow2[u]] < l:
                j += pow2[u]
        while j < k and s0[j] < l:
            j += 1
        ans0 = ans0 * tree3[j] % mod
    last = ans0
    ans.append(ans0)
sys.stdout.write('\n'.join(map(str, ans)))

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def fast_prime_factorization(n):
    d = [0 if i & 1 else 2 for i in range(n + 1)]
    (d[0], d[1]) = (0, 1)
    for i in range(3, n + 1):
        if d[i]:
            continue
        for j in range(i, n + 1, 2 * i):
            if not d[j]:
                d[j] = i
    return d

def f(u, v):
    return u * l2 + v

def g(u, v):
    return u * l + v

def get_segment(s, t):
    (s, t) = (s + l1, t + l1)
    ans = []
    while s <= t:
        if s & 1:
            ans.append(s)
            s += 1
        s >>= 1
        if not t & 1:
            ans.append(t)
            t -= 1
        t >>= 1
    return ans
n = int(input())
mod = pow(10, 9) + 7
l = 2 * pow(10, 5) + 5
a = [0] + list(map(int, input().split()))
d = fast_prime_factorization(l)
(x, z, c) = ([0] * l, [], 0)
h = 58
for i in range(2, h):
    if d[i] == i:
        x[i] = c
        z.append(i)
        c += 1
b = [0] * (n + 1)
l1 = pow(2, (n + 1).bit_length())
l2 = 2 * l1
tree1 = [1] * (16 * l2)
v = [[] for _ in range(n + 1)]
la = [0] * l
for i in range(1, n + 1):
    u = a[i]
    v0 = []
    while u ^ d[u]:
        if d[u] < h:
            tree1[f(x[d[u]], i + l1)] *= d[u]
        else:
            v0.append(d[u])
        u //= d[u]
    if 2 <= u <= h:
        tree1[f(x[u], i + l1)] *= d[u]
    elif u <= 450:
        v0.append(u)
    elif 450 < u:
        b[i] = u
    if v0:
        if len(v0) == 1 or v0[0] ^ v0[1]:
            for j in v0:
                if la[j]:
                    v[la[j]].append(g(i, j))
                else:
                    v[0].append(g(i, j))
                la[j] = i
        else:
            k = v0[0]
            v0[1] *= k
            for j in v0:
                if la[j]:
                    v[la[j]].append(g(i, k))
                else:
                    v[0].append(g(i, k))
                la[j] = i
for i in range(0, 16 * l2, l2):
    for j in range(l1 - 1, 0, -1):
        tree1[i + j] = max(tree1[i + 2 * j], tree1[i + 2 * j + 1])
tree2 = [1] * l2
nx = [0] * (n + 1)
la = [0] * l
for i in range(1, n + 1):
    u = b[i]
    if not u:
        continue
    if la[u]:
        nx[la[u]] = i
    else:
        tree2[i + l1] = u
    la[u] = i
for i in v[0]:
    (j, k) = divmod(i, l)
    tree2[j + l1] = k * tree2[j + l1] % mod
for i in range(l1 - 1, 0, -1):
    tree2[i] = tree2[2 * i] * tree2[2 * i + 1] % mod
now = [1] * l2
for i in range(1, n + 1):
    if nx[i]:
        u = nx[i] + l1
        while u:
            now[u] += 1
            u >>= 1
    for j in v[i]:
        u = j // l + l1
        while u:
            now[u] += 1
            u >>= 1
now[0] = 0
for i in range(1, l2):
    now[i] += now[i - 1]
now = [0] + now
tree3 = [1] * now[l2]
s0 = [0] * now[l2]
for i in range(1, l2):
    tree3[now[i]] = tree2[i]
for i in range(1, n + 1):
    if nx[i]:
        u = nx[i] + l1
        s0[now[u]] = i
        now[u] += 1
        tree2[u] = b[i] * tree2[u] % mod
        tree3[now[u]] = tree2[u]
        u >>= 1
        while u:
            s0[now[u]] = i
            now[u] += 1
            tree2[u] = tree2[2 * u] * tree2[2 * u + 1] % mod
            tree3[now[u]] = tree2[u]
            u >>= 1
    for j in v[i]:
        (u, k) = divmod(j, l)
        u += l1
        s0[now[u]] = i
        now[u] += 1
        tree2[u] = k * tree2[u] % mod
        tree3[now[u]] = tree2[u]
        u >>= 1
        while u:
            s0[now[u]] = i
            now[u] += 1
            tree2[u] = tree2[2 * u] * tree2[2 * u + 1] % mod
            tree3[now[u]] = tree2[u]
            u >>= 1
for i in range(1, l2):
    s0[now[i]] = n
    now[i] += 1
now = [0] + now
q = int(input())
ans = []
last = 0
pow2 = [1]
for _ in range(20):
    pow2.append(2 * pow2[-1])
for _ in range(q):
    (x, y) = map(int, input().split())
    (l, r) = ((last + x) % n + 1, (last + y) % n + 1)
    if l > r:
        (l, r) = (r, l)
    ans0 = 1
    s = get_segment(l, r)
    for i in range(16):
        m = 0
        for j in s:
            if m < tree1[f(i, j)]:
                m = tree1[f(i, j)]
        ans0 = ans0 * m % mod
    for i in s:
        (j, k) = (now[i], now[i + 1])
        c = (k - j).bit_length()
        for u in range(c, -1, -1):
            if j + pow2[u] < k and s0[j + pow2[u]] < l:
                j += pow2[u]
        while j < k and s0[j] < l:
            j += 1
        ans0 = ans0 * tree3[j] % mod
    last = ans0
    ans.append(ans0)
sys.stdout.write('\n'.join(map(str, ans)))

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def fast_prime_factorization(n):
    d = [0 if i & 1 else 2 for i in range(n + 1)]
    (d[0], d[1]) = (0, 1)
    for i in range(3, n + 1):
        if d[i]:
            continue
        for j in range(i, n + 1, 2 * i):
            if not d[j]:
                d[j] = i
    return d

def f(u, v):
    return u * l2 + v

def g(u, v):
    return u * l + v

def get_segment(s, t):
    (s, t) = (s + l1, t + l1)
    ans = []
    while s <= t:
        if s & 1:
            ans.append(s)
            s += 1
        s >>= 1
        if not t & 1:
            ans.append(t)
            t -= 1
        t >>= 1
    return ans
n = int(input())
mod = pow(10, 9) + 7
l = 2 * pow(10, 5) + 5
a = [0] + list(map(int, input().split()))
d = fast_prime_factorization(l)
(x, z, c) = ([0] * l, [], 0)
h = 60
for i in range(2, h):
    if d[i] == i:
        x[i] = c
        z.append(i)
        c += 1
b = [0] * (n + 1)
l1 = pow(2, (n + 1).bit_length())
l2 = 2 * l1
tree1 = [1] * (20 * l2)
v = [[] for _ in range(n + 1)]
la = [0] * l
for i in range(1, n + 1):
    u = a[i]
    v0 = []
    while u ^ d[u]:
        if d[u] < h:
            tree1[f(x[d[u]], i + l1)] *= d[u]
        else:
            v0.append(d[u])
        u //= d[u]
    if 2 <= u <= h:
        tree1[f(x[u], i + l1)] *= d[u]
    elif u <= 450:
        v0.append(u)
    elif 450 < u:
        b[i] = u
    if v0:
        if len(v0) == 1 or v0[0] ^ v0[1]:
            for j in v0:
                if la[j]:
                    v[la[j]].append(g(i, j))
                else:
                    v[0].append(g(i, j))
                la[j] = i
        else:
            k = v0[0]
            v0[1] *= k
            for j in v0:
                if la[j]:
                    v[la[j]].append(g(i, k))
                else:
                    v[0].append(g(i, k))
                la[j] = i
for i in range(0, 20 * l2, l2):
    for j in range(l1 - 1, 0, -1):
        tree1[i + j] = max(tree1[i + 2 * j], tree1[i + 2 * j + 1])
tree2 = [1] * l2
nx = [0] * (n + 1)
la = [0] * l
for i in range(1, n + 1):
    u = b[i]
    if not u:
        continue
    if la[u]:
        nx[la[u]] = i
    else:
        tree2[i + l1] = u
    la[u] = i
for i in v[0]:
    (j, k) = divmod(i, l)
    tree2[j + l1] *= k
for i in range(l1 - 1, 0, -1):
    tree2[i] = tree2[2 * i] * tree2[2 * i + 1] % mod
now = [1] * l2
for i in range(1, n + 1):
    if nx[i]:
        u = nx[i] + l1
        while u:
            now[u] += 1
            u >>= 1
    for j in v[i]:
        u = j // l + l1
        while u:
            now[u] += 1
            u >>= 1
now[0] = 0
for i in range(1, l2):
    now[i] += now[i - 1]
now = [0] + now
tree3 = [1] * now[l2]
s0 = [0] * now[l2]
for i in range(1, l2):
    tree3[now[i]] = tree2[i]
for i in range(1, n + 1):
    if nx[i]:
        u = nx[i] + l1
        s0[now[u]] = i
        now[u] += 1
        tree2[u] *= b[i]
        tree3[now[u]] = tree2[u]
        u >>= 1
        while u:
            s0[now[u]] = i
            now[u] += 1
            tree2[u] = tree2[2 * u] * tree2[2 * u + 1] % mod
            tree3[now[u]] = tree2[u]
            u >>= 1
    for j in v[i]:
        (u, k) = divmod(j, l)
        u += l1
        s0[now[u]] = i
        now[u] += 1
        tree2[u] *= k
        tree3[now[u]] = tree2[u]
        u >>= 1
        while u:
            s0[now[u]] = i
            now[u] += 1
            tree2[u] = tree2[2 * u] * tree2[2 * u + 1] % mod
            tree3[now[u]] = tree2[u]
            u >>= 1
for i in range(1, l2):
    s0[now[i]] = n
    now[i] += 1
now = [0] + now
q = int(input())
ans = []
last = 0
pow2 = [1]
for _ in range(20):
    pow2.append(2 * pow2[-1])
for _ in range(q):
    (x, y) = map(int, input().split())
    (l, r) = ((last + x) % n + 1, (last + y) % n + 1)
    if l > r:
        (l, r) = (r, l)
    ans0 = 1
    s = get_segment(l, r)
    for i in range(20):
        m = 0
        for j in s:
            if m < tree1[f(i, j)]:
                m = tree1[f(i, j)]
        ans0 = ans0 * m % mod
    for i in s:
        (j, k) = (now[i], now[i + 1])
        c = (k - j).bit_length()
        for u in range(c, -1, -1):
            if j + pow2[u] < k and s0[j + pow2[u]] < l:
                j += pow2[u]
        while j < k and s0[j] < l:
            j += 1
        ans0 = ans0 * tree3[j] % mod
    last = ans0
    ans.append(ans0)
sys.stdout.write('\n'.join(map(str, ans)))