套路题。
考虑根号分治,\(\le \sqrt{V} = 447\) 的质因子直接暴力 ST 表维护。对于 \(> \sqrt{V}\) 的质因子每个数最多有一个。记 \(big_i\) 为 \(a_i > \sqrt{V}\) 的质因子,维护 \(pre_i\) 表示上一个使得 \(big_i = big_j\) 的 \(j\)(若 \(big_i\) 不存在则忽略),则所求即为 \(\prod\limits_{i \in [l,r] \land pre_i \le l - 1} big_i\)。随便主席树维护即可。
时间复杂度 \(O(kn \log n)\),其中 \(k = \sum\limits_{i \le \sqrt{V} \and i \in prime} 1\)。
code
// Problem: F. Boring Queries
// Contest: Codeforces - Codeforces Round 675 (Div. 2)
// URL: https://codeforces.com/problemset/problem/1422/F
// Memory Limit: 512 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;
const int maxn = 100100, logn = 18;
const int N = 200000, B = 447, K = 86;
const ll mod = 1000000007;
inline ll qpow(ll b, ll p) {
ll res = 1;
while (p) {
if (p & 1) {
res = res * b % mod;
}
b = b * b % mod;
p >>= 1;
}
return res;
}
int n, m, a[maxn], pr[maxn], tot, lg[maxn], big[maxn], pre[maxn];
ll pw[K + 5][100];
bool vis[maxn << 1];
vector<int> vc[maxn];
struct ST {
char f[maxn][logn];
void init(int p) {
for (int i = 1; i <= n; ++i) {
int x = a[i];
while (x % p == 0) {
x /= p;
++f[i][0];
}
}
for (int j = 1; (1 << j) <= n; ++j) {
for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
}
}
}
inline int query(int l, int r) {
int k = lg[r - l + 1];
return (int)max(f[l][k], f[r - (1 << k) + 1][k]);
}
} st[87];
void init() {
for (int i = 2; i <= N; ++i) {
if (!vis[i]) {
pr[++tot] = i;
}
for (int j = 1; j <= tot && i * pr[j] <= N; ++j) {
vis[i * pr[j]] = 1;
if (i % pr[j] == 0) {
break;
}
}
}
}
int ls[maxn << 6], rs[maxn << 6], rt[maxn], ntot;
ll tree[maxn << 6];
int build(int l, int r) {
int rt = ++ntot;
tree[rt] = 1;
if (l == r) {
return rt;
}
int mid = (l + r) >> 1;
ls[rt] = build(l, mid);
rs[rt] = build(mid + 1, r);
return rt;
}
int update(int rt, int l, int r, int x) {
int dir = ++ntot;
ls[dir] = ls[rt];
rs[dir] = rs[rt];
tree[dir] = tree[rt] * big[x] % mod;
if (l == r) {
return dir;
}
int mid = (l + r) >> 1;
x <= mid ? (ls[dir] = update(ls[rt], l, mid, x)) : (rs[dir] = update(rs[rt], mid + 1, r, x));
return dir;
}
ll query(int rt, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) {
return tree[rt];
}
int mid = (l + r) >> 1;
ll res = 1;
if (ql <= mid) {
res = res * query(ls[rt], l, mid, ql, qr) % mod;
}
if (qr > mid) {
res = res * query(rs[rt], mid + 1, r, ql, qr) % mod;
}
return res;
}
void solve() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
int x = a[i];
for (int j = 1; j <= K; ++j) {
while (x % pr[j] == 0) {
x /= pr[j];
}
}
if (x != 1) {
big[i] = x;
}
}
scanf("%d", &m);
for (int i = 2; i <= n; ++i) {
lg[i] = lg[i >> 1] + 1;
}
for (int i = 1; i <= K; ++i) {
st[i].init(pr[i]);
}
map<int, int> mp;
for (int i = 1; i <= n; ++i) {
if (big[i]) {
pre[i] = mp[big[i]];
mp[big[i]] = i;
vc[pre[i]].pb(i);
}
}
int tr = build(1, n);
for (int i = 0; i < n; ++i) {
rt[i] = (i ? rt[i - 1] : tr);
for (int x : vc[i]) {
rt[i] = update(rt[i], 1, n, x);
}
}
for (int i = 1; i <= K; ++i) {
pw[i][0] = 1;
for (int j = 1; j < 100; ++j) {
pw[i][j] = pw[i][j - 1] * pr[i] % mod;
}
}
ll lstans = 0;
while (m--) {
int l, r;
scanf("%d%d", &l, &r);
l = (l + lstans) % n + 1;
r = (r + lstans) % n + 1;
if (l > r) {
swap(l, r);
}
ll ans = query(rt[l - 1], 1, n, l, r);
for (int i = 1; i <= K; ++i) {
ans = ans * pw[i][st[i].query(l, r)] % mod;
}
printf("%lld\n", lstans = ans);
}
}
int main() {
init();
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}