PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000197. Gấu và các số nguyên tố (Bear and Prime Numbers)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Gấu và các số nguyên tố (Bear and Prime Numbers)

Gấu và các số nguyên tố (Bear and Prime Numbers)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You receive x1,…,xnx_1,\ldots,x_n. For every prime pp, define f(p)f(p) as the number of positions ii such that p∣xip\mid x_i; duplicate values count multiple times. Each query [l,r][l,r] asks for ∑p prime, l≤p≤rf(p)\sum_{p\ \mathrm{prime},\ l\le p\le r}f(p). Query endpoints may exceed every array value; such primes contribute zero.

Input

First line: nn; second line: the nn values xix_i; third line: mm; then mm lines l,rl,r.

Output

Print one answer per query, in input order.

Subtasks

  • Subtask 1 (20 points): 1≤n≤10001\le n\le 1000; 2≤xi≤10002\le x_i\le 1000; 1≤m≤1001\le m\le 100; 2≤li≤ri≤2⋅1092\le l_i\le r_i\le 2\cdot10^9.

  • Subtask 2 (30 points): 1≤n≤1000001\le n\le 100000; 2≤xi≤10000002\le x_i\le 1000000; 1≤m≤50001\le m\le 5000; 2≤li≤ri≤2⋅1092\le l_i\le r_i\le 2\cdot10^9.

  • Subtask 3 (50 points): 1≤n≤10000001\le n\le 1000000; 2≤xi≤100000002\le x_i\le 10000000; 1≤m≤500001\le m\le 50000; 2≤li≤ri≤2⋅1092\le l_i\le r_i\le 2\cdot10^9.

Examples

Example 1

Input

6
5 5 7 10 14 15
3
2 11
3 12
4 4

Output

9
7
0

Explanation

The values for primes 2,3,5,7,112,3,5,7,11 are 2,1,4,2,02,1,4,2,0, totaling 99 in the first query. No prime lies in [4,4][4,4].

Example 2

Input

7
2 3 5 7 11 4 8
2
8 10
2 123

Output

0
7

Explanation

The range [8,10][8,10] contains no prime. The second query covers all prime factors appearing in the array, resulting in 77.

Example 3

Input

3
6 6 6
2
2 2
3 3

Output

3
3

Explanation

All three occurrences of 66 are divisible by each of the primes 22 and 33, yielding three for either query.