33OJ
登录 注册

#P17899. [ICPC 2026 Nanchang I] Little Z's Function

    ID: 20635 远端评测题 5500ms 1024MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>线段树树状数组江西扫描线ICPC2026省赛/邀请赛欧拉函数

[ICPC 2026 Nanchang I] Little Z's Function

Problem Description

Little Z was obsessed with number-theoretic functions such as Euler's totient function φ\varphi (Note: φ(n)\varphi(n) denotes the number of positive integers not exceeding nn that are coprime to nn.) back in high school.

One day, Little Z decides to play a game with his robotic friend. First, Little Z gives a permutation (Note: A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).) pp of length nn. Then he asks his friend multiple queries. For a given interval [l,r][l, r] (1≤l<r≤n1 \le l < r \le n), find the maximum value of φ(pi×pj)\varphi(p_i \times p_j) where l≤i<j≤rl \le i < j \le r.

Formally, for each query with two integers l,rl, r, you need to compute:

$$\max_{l \le i < j \le r} \varphi ( p_i \times p_j )$$

Little Z has solved exactly 00 queries. Please help him compute the remaining qq queries.

Input Format

The first line contains a positive integer nn (2≤n≤5×1052 \le n \le 5 \times 10^5) --- the length of the permutation.

The second line contains nn distinct integers p1,p2,…,pnp_1, p_2, \ldots, p_n (1≤pi≤n1 \le p_i \le n) --- the given permutation pp.

The third line contains a positive integer qq (1≤q≤5×1051 \le q \le 5 \times 10^5) --- the number of queries.

Each of the next qq lines contains two integers lil_i and rir_i (1≤li<ri≤n1 \le l_i < r_i \le n) --- the endpoints of the ii-th query.

Output Format

For each query, output a single integer --- the maximum value.

6
3 2 5 4 1 6
8
4 5
2 6
3 5
2 3
3 4
1 5
4 5
2 6
2
8
8
4
8
8
2
8